<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta property="og:type" content="website">
<meta property="og:title" content="kyssion-blog">
<meta property="og:url" content="http://yoursite.com/page/9/index.html">
<meta property="og:site_name" content="kyssion-blog">
<meta property="og:locale" content="zh-Hans">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="kyssion-blog">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/page/9/"/>





  <title>kyssion-blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left 
  page-home">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">kyssion-blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-schedule">
          <a href="/schedule/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-calendar"></i> <br />
            
            日程表
          </a>
        </li>
      
        
        <li class="menu-item menu-item-sitemap">
          <a href="/sitemap.xml" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-sitemap"></i> <br />
            
            站点地图
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
  <section id="posts" class="posts-expand">
    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/10/09/spring-boot2.0(3)配置读取/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="kyssion">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="kyssion-blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/10/09/spring-boot2.0(3)配置读取/" itemprop="url">未命名</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-10-09T15:22:37+08:00">
                2018-10-09
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>导引： spring boot 总体上提供了三种外化配置 1. 使用@Value注释将属性值直接注入到bean中 2. pring的Environment抽象访问 3. 通过@ConfigurationProperties绑定到结构化对象，这里介绍一下</p>
<h3 id="spring-boot-可以提供参数方式和覆盖原则"><a href="#spring-boot-可以提供参数方式和覆盖原则" class="headerlink" title="spring boot 可以提供参数方式和覆盖原则"></a>spring boot 可以提供参数方式和覆盖原则</h3><p>spring boot 可以通过如下的方法倒入相应的属性之，并且按照如下从上到下的顺序进行覆盖</p>
<ol>
<li>Devtools 主目录上的全局设置属性（~/.spring-boot-devtools.properties当devtools处于活动状态时）。</li>
<li>@TestPropertySource 测试上的注释。</li>
<li>@SpringBootTest#properties 测试中的注释属性。</li>
<li>命令行参数。</li>
<li>来自SPRING_APPLICATION_JSON（嵌入在环境变量或系统属性中的内联JSON）的属性。</li>
<li>ServletConfig init参数。</li>
<li>ServletContext init参数。</li>
<li>JNDI属性来自java:comp/env。</li>
<li>Java系统属性（System.getProperties()）。</li>
<li>OS环境变量。</li>
<li>一RandomValuePropertySource，只有在拥有性能random.*。</li>
<li>特定于配置文件的应用程序属性在打包的jar（application-{profile}.properties和YAML变体）之外。</li>
<li>打包在jar中的特定于配置文件的应用程序属性（application-{profile}.properties 以及YAML变体）。</li>
<li>应用程序属性在打包的jar之外（application.properties和YAML变体）。</li>
<li>打包在jar中的应用程序属性（application.properties和YAML变体）。</li>
<li>@PropertySource 你的@Configuration课上的注释。</li>
<li>默认属性（由设置指定SpringApplication.setDefaultProperties）。</li>
</ol>
<p>提供一个简单的使用内置属性的列子</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> org.springframework.stereotype.*;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.*;</span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyBean</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Value</span>(<span class="string">"$&#123;name&#125;"</span>)</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>@Value 将会将环境中的name参数注入到MyBean的name属性中</p>
<blockquote>
<p>注意: 这里解释一下上面的第五条： SPRING_APPLICATION_JSON 这个属性是一个环境变量属性，可以在环境中进行相关的配置，或者直接使用命令行$ SPRING_APPLICATION_JSON=’{“acme”:{“name”:”test”}}’ java -jar myapp.jar。</p>
</blockquote>
<blockquote>
<p>注意： json的方式解析在spring boot 中拥有两个地方，第一个上面的第9条：java -Dspring.application.json=’{“name”:”test”}’ -jar myapp.jar 。第二个上面的第4条，$ java -jar myapp.jar –spring.application.json=’{“name”:”test”}’</p>
</blockquote>
<blockquote>
<p>注意： 解释上面的11 条随机参数</p>
</blockquote>
<p>这个方法可以生成随机参数可以生成整数，长整数，uuids或字符串，如以下示例所示：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">my.secret=$&#123;random.value&#125;</span><br><span class="line">my.number=$&#123;random.int&#125;</span><br><span class="line">my.bignumber=$&#123;random.long&#125;</span><br><span class="line">my.uuid=$&#123;random.uuid&#125;</span><br><span class="line">my.number.less.than.ten=$&#123;random.int(10)&#125;</span><br><span class="line">my.number.in.range=$&#123;random.int[1024,65536]&#125;</span><br></pre></td></tr></table></figure>
<p>注意最后两个使用了表达式的方法，int[start,end] 表示从start到end区间随机取数 int(max) 这种方式表示取的值不大于max</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/10/09/spring-boot2.0(2)监听器、事件/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="kyssion">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="kyssion-blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/10/09/spring-boot2.0(2)监听器、事件/" itemprop="url">未命名</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-10-09T15:22:37+08:00">
                2018-10-09
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>导引： 1. spring boot监听事件的使用 2. spring boot 内置监听事件初始化applicationContext 3. CommandLineRunner接口和ApplicationRunner类</p>
<h3 id="spring-boot-中的上下文监听器和事件"><a href="#spring-boot-中的上下文监听器和事件" class="headerlink" title="spring boot 中的上下文监听器和事件"></a>spring boot 中的上下文监听器和事件</h3><p>spring boot的事件和 spring原生的事件没有什么差别但是，spring boot 有一些自己的事件</p>
<ol>
<li>ApplicationStartingEvent在运行开始时但在任何处理之前发送，除了注册侦听器和初始化器之外。</li>
<li>ApplicationEnvironmentPreparedEvent当在上下文中使用的环境是已知的但在创建上下文之前发送。</li>
<li>ApplicationPreparedEvent在刷新开始之前但在bean定义加载之后发送。</li>
<li>ApplicationStartedEvent在刷新上下文之后但在调用任何应用程序和命令行参赛者之前发送。</li>
<li>ApplicationReadyEvent在任何应用程序和命令行参数被调用后发送。 它表示应用程序已准备好为请求提供服务。</li>
<li>ApplicationFailedEvent如果启动时出现异常，则发送。</li>
</ol>
<p>注意有些事件可能是在applicatincontext之前就出发了，所以不能使用@bean的方法注册在容器中，在spring boot 中可以应如下的方法注册监听器,使用方法：SpringApplication.addListeners(…​)， SpringApplicationBuilder.listeners(…​)，配置文件中添加：META-INF/spring.factories 中 org.springframework.context.ApplicationListener</p>
<p>注意了：因为spring拥有上下文的关系，而监听器除了监听自己的时间还会监听子元素的事件，所以在实现监听器的时候官方推荐同时实现ApplicationContextAware接口，来比较事件的context和和自身context的关系。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyApplicationListener</span> <span class="keyword">implements</span> <span class="title">ApplicationListener</span>&lt;<span class="title">ApplicationEvent</span>&gt;</span>&#123;</span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onApplicationEvent</span><span class="params">(ApplicationEvent arg0)</span> </span>&#123;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>总结添加事件的三种方法</p>
</blockquote>
<ol>
<li>@bean</li>
<li>SpringApplication.addListeners(…​)， SpringApplicationBuilder.listeners(…​)</li>
<li>配置文件中添加：META-INF/spring.factories 中 org.springframework.context.ApplicationListener</li>
</ol>
<h3 id="spring-boot-应用环境的判断构建ApplicationContext"><a href="#spring-boot-应用环境的判断构建ApplicationContext" class="headerlink" title="spring boot 应用环境的判断构建ApplicationContext"></a>spring boot 应用环境的判断构建ApplicationContext</h3><p>我们在没有使用spring boot 单纯的使用spring 或者使用spring mvc 的时候通常使用的application是XmlServletWebServerApplicationContext，或者ClasspathXmlApplicationContext ， 同样spring boot 基于注解方法使用了一套特殊的context</p>
<ol>
<li><p>首先spring boot 判断这个环境是不是spring mvc 环境如果是马么webappcationcontext 选择AnnotationConfigServletWebServerApplicationContext</p>
</li>
<li><p>否则判断是否是webflux 环境如果是 就使用AnnotationConfigReactiveWebApplicationContext</p>
</li>
<li><p>最后如果都不是，使用AnnotationConfigApplicationContext</p>
</li>
</ol>
<p>这样有一个问题，如果是spring mvc 和spring webflux 同时使用的时候将会造成使用springmvc 的环境，为了解决这个问题，可以使用setWebApplicationType(WebApplicationType)方法，或者使用setApplicationContextClass(…​)<strong>有待确认</strong>.</p>
<h3 id="spring-bootW启动前执行特定的程序"><a href="#spring-bootW启动前执行特定的程序" class="headerlink" title="spring bootW启动前执行特定的程序"></a>spring bootW启动前执行特定的程序</h3><p>在spring boot中 ， spring boot 会在容器彻底启动前运行实现现CommandLineRunner接口的bean或者ApplicationRunner接口的bean</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> org.springframework.boot.*;</span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.*;</span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyBean</span> <span class="keyword">implements</span> <span class="title">CommandLineRunner</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">(String... args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// Do something...</span></span><br><span class="line">		args就是main传入的string数组</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Runtest</span> <span class="keyword">implements</span> <span class="title">ApplicationRunner</span> </span>&#123;</span><br><span class="line">	<span class="comment">//自动注入进来</span></span><br><span class="line">	<span class="meta">@AutoWrite</span></span><br><span class="line">	ApplicationArguments run;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">(ApplicationArguments args)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        <span class="comment">// Do something...</span></span><br><span class="line">		这里传入的是spring 对传入数据的封装</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>注意这两个接口其中第二个接口传递命令的额外参数使用的是ApplicationArguments这个类（第一个和main相同）</p>
<blockquote>
<p>注意如果系统想使用，运行的时候传入的数据的话，可以使用ApplicationArguments 这个参数将相关的值注入进来或者使用ApplicationRunner接口或者CommandLineRunner初始化</p>
</blockquote>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/10/09/spring5-SpEL表达式/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="kyssion">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="kyssion-blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/10/09/spring5-SpEL表达式/" itemprop="url">未命名</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-10-09T15:22:37+08:00">
                2018-10-09
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="spring5-SpEL表达式"><a href="#spring5-SpEL表达式" class="headerlink" title="spring5-SpEL表达式"></a>spring5-SpEL表达式</h2><p>SpEL是一种强大的、简洁的装配Bean的方式，它通过运行期执行的表达式将值装配到Bean的属性或构造器参数中。比如说 xml文件中动态的加入上下文信息，使用类的静态方法等相关方法载入参数</p>
<h3 id="载入基本类型"><a href="#载入基本类型" class="headerlink" title="载入基本类型"></a>载入基本类型</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"count"</span> <span class="attr">value</span>=<span class="string">"#&#123;5&#125;"</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"frequency"</span> <span class="attr">value</span>=<span class="string">"#&#123;89.7&#125;"</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"capacity"</span> <span class="attr">value</span>=<span class="string">"#&#123;1e4&#125;"</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"name"</span> <span class="attr">value</span>=<span class="string">"#&#123;'moonlit'&#125;"</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"name"</span> <span class="attr">value</span>=<span class="string">'#&#123;"moonlit"&#125;'</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"enabled"</span> <span class="attr">value</span>=<span class="string">"#&#123;true&#125;"</span> /&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="使用xml配置文件中的其他配置选项"><a href="#使用xml配置文件中的其他配置选项" class="headerlink" title="使用xml配置文件中的其他配置选项"></a>使用xml配置文件中的其他配置选项</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"poem"</span> <span class="attr">class</span>=<span class="string">"com.moonlit.myspring.Poem"</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"poet"</span> <span class="attr">class</span>=<span class="string">"com.moonlit.myspring.Poet"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"poem"</span> <span class="attr">value</span>=<span class="string">"#&#123;poem&#125;"</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>获取poet中传入的poem对象的两种方法</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">#&#123;poet.poem&#125;</span><br></pre></td></tr></table></figure>
<h3 id="调用方法"><a href="#调用方法" class="headerlink" title="调用方法"></a>调用方法</h3><p>调用其他 名字为poet bean的方法</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">#&#123;poet.getPoem()&#125;</span><br><span class="line">#&#123;songSelector.selectSong()?.toUpperCase()&#125;</span><br></pre></td></tr></table></figure>
<p>上面使用 ?. 运算符代替点（.）来访问toUpperCase()方法。在访问邮编方法之前，该运算符会确保左边项的值不为null。所以，如果selectorSong返回null，SpEL就不再尝试调用toUpperCase()方法。</p>
<p>在SpEL中，使用T()运算符会调用类作用域的方法和常量</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">#&#123;T(java.lang.Math).PI&#125;</span><br><span class="line">#&#123;T(java.lang.Math).random()&#125;</span><br></pre></td></tr></table></figure>
<h3 id="使用逻辑算数运行"><a href="#使用逻辑算数运行" class="headerlink" title="使用逻辑算数运行"></a>使用逻辑算数运行</h3><p>算数表达式</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">#&#123;circle.radius + 100.0&#125;</span><br></pre></td></tr></table></figure>
<p>操作一个表达式的值：eq(==),lt(&lt;),le(&lt;=),gt(&gt;),ge(&gt;=)。逻辑表达式：and,or,not或!。条件运算符：使用三元运算符 ？：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">#&#123;sonSelector.selecSOng()==&apos;Jingle Bells&apos;?piano:saxophone&#125;</span><br><span class="line">#&#123;kenny.song != null ? kenny.song : &apos;Greensleeves&apos;&#125;</span><br><span class="line">#&#123;kenny.song ?: &apos;Greensleeves&apos;&#125;</span><br></pre></td></tr></table></figure>
<p>正则表达式匹配</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">#&#123;admin.email matches &apos;[a-zA-Z0-9._%+_]+@[a-zA-Z0-9.-]+\\.com&apos;&#125;</span><br></pre></td></tr></table></figure>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/10/09/spring-boot2.0(1)启动、运行和结束/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="kyssion">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="kyssion-blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/10/09/spring-boot2.0(1)启动、运行和结束/" itemprop="url">未命名</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-10-09T15:22:37+08:00">
                2018-10-09
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>导引： spring boot的简单配置，基本启动方法，和特殊关闭模式</p>
<h3 id="springboot-配置的核心元素"><a href="#springboot-配置的核心元素" class="headerlink" title="springboot 配置的核心元素"></a>springboot 配置的核心元素</h3><p>spring 只用maven的parent继承的方法（gradle类似）进行依赖管理和使用maven插件（gradle同样类似）的方法生成可执行的jar文件</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span><br><span class="line"><span class="tag">&lt;<span class="name">project</span> <span class="attr">xmlns</span>=<span class="string">"http://maven.apache.org/POM/4.0.0"</span> <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></span></span><br><span class="line"><span class="tag">	<span class="attr">xsi:schemaLocation</span>=<span class="string">"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">modelVersion</span>&gt;</span>4.0.0<span class="tag">&lt;/<span class="name">modelVersion</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.example<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>myproject<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">version</span>&gt;</span>0.0.1-SNAPSHOT<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--使用继承的方法实现自动化配置--&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">parent</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-parent<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">version</span>&gt;</span>2.0.1.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">parent</span>&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- Additional lines to be added here... --&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--使用maven插件实现自动化配置--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">build</span>&gt;</span></span><br><span class="line">	    <span class="tag">&lt;<span class="name">plugins</span>&gt;</span></span><br><span class="line">		    <span class="tag">&lt;<span class="name">plugin</span>&gt;</span></span><br><span class="line">			    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">			    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-maven-plugin<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">		    <span class="tag">&lt;/<span class="name">plugin</span>&gt;</span></span><br><span class="line">	    <span class="tag">&lt;/<span class="name">plugins</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">build</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">project</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>当不使用parent进行依赖配置的时候可以使用dependencyManagement来进行管理</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependencyManagement</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span><br><span class="line">		    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">			    <span class="comment">&lt;!-- Import dependency management from Spring Boot --&gt;</span></span><br><span class="line">			    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">			    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-dependencies<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">			    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.0.1.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">			    <span class="tag">&lt;<span class="name">type</span>&gt;</span>pom<span class="tag">&lt;/<span class="name">type</span>&gt;</span></span><br><span class="line">			    <span class="tag">&lt;<span class="name">scope</span>&gt;</span>import<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependencyManagement</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="springBoot-starter-springBoot可以实现自动化配置的核心方法"><a href="#springBoot-starter-springBoot可以实现自动化配置的核心方法" class="headerlink" title="springBoot starter-springBoot可以实现自动化配置的核心方法"></a>springBoot starter-springBoot可以实现自动化配置的核心方法</h3><p>springBoot提供了一系列的starter方便进行自动化配置，比如spring-boot-starter-web</p>
<blockquote>
<p>springBoot starter 明明规则：如果是官法的starter 将会按照spring-boot-starter-<em> 来进行划分，如果是自己实现的starter方法，建议按照</em>-spring-boot-starter 来进行命名</p>
</blockquote>
<p>这里介绍一个特边的非应用starter，spring-boot-starter-actuator，这个starter实现了监控的功能</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-actuator<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>spring boot 提供了非常多的配置选项，具体的可以到官网上查询</p>
</blockquote>
<h3 id="spring-boot-热部署工具-Developer-Tools"><a href="#spring-boot-热部署工具-Developer-Tools" class="headerlink" title="spring boot 热部署工具  Developer Tools"></a>spring boot 热部署工具  Developer Tools</h3><p>spring boot 实现了一定程度下的热部署功能，暂时不做过多的讨论<br>配置<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&lt;dependencies&gt;配置</span><br><span class="line">	&lt;dependency&gt;配置</span><br><span class="line">		&lt;groupId&gt;org.springframework.boot配置&lt;/groupId&gt;</span><br><span class="line">		&lt;artifactId&gt;spring-boot-devtools&lt;配置/artifactId&gt;</span><br><span class="line">		&lt;optional&gt;true&lt;/optional&gt;</span><br><span class="line">	&lt;/dependency&gt;</span><br><span class="line">&lt;/dependencies&gt;</span><br></pre></td></tr></table></figure></p>
<h3 id="spring-boot-的启动方法"><a href="#spring-boot-的启动方法" class="headerlink" title="spring boot 的启动方法"></a>spring boot 的启动方法</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">	SpringApplication app = <span class="keyword">new</span> SpringApplication(MySpringConfiguration.class);</span><br><span class="line">	app.setBannerMode(Banner.Mode.OFF);</span><br><span class="line">	app.run(args);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>注意这里传递过去的参数是一个配置源，也就是说这个类必须是@Configuration或者是一个继承@Configuration注解标记的类</p>
<p>当然，也可以通过使用application.properties文件来配置SpringApplication，但是这里涉及到springboot外部化配置，见下面</p>
<p>springboot 还可以使用流式布局构建application的上下文关系，具体的实现如下</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">new</span> SpringApplicationBuilder()</span><br><span class="line">		.sources(Parent.class)</span><br><span class="line">		.child(Application.class)</span><br><span class="line">		.bannerMode(Banner.Mode.OFF)</span><br><span class="line">		.run(args);</span><br></pre></td></tr></table></figure>
<p>注意：Web组件必须包含在子上下文中，并且父环境和子环境都使用相同的环境</p>
<p>#spring boot 带有状态的结束 使用exit 运行程序</p>
<p>在调用SpringApplication.exit（）时希望返回特定的退出代码，那么bean可以实现org.springframework.boot.ExitCodeGenerator接口。 ，然后可以将此退出代码传递给System.exit（）以将其作为状态代码返回，</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* 接口只有一个方法</span></span><br><span class="line"><span class="comment">* <span class="doctag">@Compomet</span></span></span><br><span class="line"><span class="comment">* class Runtest implements ExitCodeGenerator &#123;</span></span><br><span class="line"><span class="comment">*     <span class="doctag">@Override</span></span></span><br><span class="line"><span class="comment">*     public int getExitCode() &#123;</span></span><br><span class="line"><span class="comment">*         return 0;</span></span><br><span class="line"><span class="comment">*     &#125;</span></span><br><span class="line"><span class="comment">* &#125;</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="meta">@SpringBootApplication</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ExitCodeApplication</span> </span>&#123;</span><br><span class="line">	<span class="meta">@Bean</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> ExitCodeGenerator <span class="title">exitCodeGenerator</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> () -&gt; <span class="number">42</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		System.exit(SpringApplication</span><br><span class="line">				.exit(SpringApplication.run(ExitCodeApplication.class, args)));</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/10/09/spring5-SpEL表达式的使用/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="kyssion">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="kyssion-blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/10/09/spring5-SpEL表达式的使用/" itemprop="url">未命名</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-10-09T15:22:37+08:00">
                2018-10-09
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="SpEL-表达式的使用"><a href="#SpEL-表达式的使用" class="headerlink" title="SpEL 表达式的使用"></a>SpEL 表达式的使用</h3><p>SpEl 表达式主要用在bean的定义是上</p>
<p>基于xml的配置</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"numberGuess"</span> <span class="attr">class</span>=<span class="string">"org.spring.samples.NumberGuess"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"randomNumber"</span> <span class="attr">value</span>=<span class="string">"#&#123; T(java.lang.Math).random() * 100.0 &#125;"</span>/&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- other properties --&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>基于注解的配置</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">FieldValueTestBean</span></span></span><br><span class="line">    @Value("#&#123; systemProperties['user.region'] &#125;")</span><br><span class="line">    <span class="keyword">private</span> String defaultLocale;</span><br><span class="line">    <span class="meta">@Value</span>(<span class="string">"#&#123; systemProperties['user.region'] &#125;"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setDefaultLocale</span><span class="params">(String defaultLocale)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.defaultLocale = defaultLocale;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getDefaultLocale</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.defaultLocale;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>基于注解的配置在使用自动装配的方法和构造函数时使用@Value注解配套方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SimpleMovieLister</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> MovieFinder movieFinder;</span><br><span class="line">    <span class="keyword">private</span> String defaultLocale;</span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">configure</span><span class="params">(MovieFinder movieFinder,</span></span></span><br><span class="line"><span class="function"><span class="params">            @Value(<span class="string">"#&#123; systemProperties['user.region'] &#125;"</span>)</span> String defaultLocale) </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.movieFinder = movieFinder;</span><br><span class="line">        <span class="keyword">this</span>.defaultLocale = defaultLocale;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="语言参考"><a href="#语言参考" class="headerlink" title="语言参考"></a>语言参考</h3><p>spring 内置了一个解析器SpelExpressionParser</p>
<h4 id="基本类型表达式"><a href="#基本类型表达式" class="headerlink" title="基本类型表达式"></a>基本类型表达式</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">ExpressionParser parser = <span class="keyword">new</span> SpelExpressionParser();</span><br><span class="line"><span class="comment">// evals to "Hello World"</span></span><br><span class="line">String helloWorld = (String) parser.parseExpression(<span class="string">"'Hello World'"</span>).getValue();</span><br><span class="line"><span class="keyword">double</span> avogadrosNumber = (Double) parser.parseExpression(<span class="string">"6.0221415E+23"</span>).getValue();</span><br><span class="line"><span class="comment">// evals to 2147483647</span></span><br><span class="line"><span class="keyword">int</span> maxValue = (Integer) parser.parseExpression(<span class="string">"0x7FFFFFFF"</span>).getValue();</span><br><span class="line"><span class="keyword">boolean</span> trueValue = (Boolean) parser.parseExpression(<span class="string">"true"</span>).getValue();</span><br><span class="line">Object nullValue = parser.parseExpression(<span class="string">"null"</span>).getValue();</span><br></pre></td></tr></table></figure>
<h4 id="获取对象的属性"><a href="#获取对象的属性" class="headerlink" title="获取对象的属性"></a>获取对象的属性</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> year = (Integer) parser.parseExpression(<span class="string">"Birthdate.Year + 1900"</span>).getValue(context);</span><br><span class="line">String city = (String) parser.parseExpression(<span class="string">"placeOfBirth.City"</span>).getValue(context);</span><br><span class="line">parser.parseExpression(<span class="string">"Officers['advisors'][0].PlaceOfBirth.Country"</span>).setValue(</span><br><span class="line">        societyContext, <span class="string">"Croatia"</span>);</span><br></pre></td></tr></table></figure>
<h4 id="获取数组的属性"><a href="#获取数组的属性" class="headerlink" title="获取数组的属性"></a>获取数组的属性</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">String invention = parser.parseExpression(<span class="string">"Members[0].Inventions[6]"</span>).getValue(</span><br><span class="line">        societyContext, String.class);</span><br><span class="line"></span><br><span class="line"><span class="comment">//创造一个内链表</span></span><br><span class="line"></span><br><span class="line">List listOfLists = (List) parser.parseExpression(<span class="string">"&#123;&#123;'a','b'&#125;,&#123;'x','y'&#125;&#125;"</span>).getValue(context);</span><br><span class="line"></span><br><span class="line"><span class="comment">//创建数组</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span>[][] numbers3 = (<span class="keyword">int</span>[][]) parser.parseExpression(<span class="string">"new int[4][5]"</span>).getValue(context);</span><br></pre></td></tr></table></figure>
<h4 id="内联map"><a href="#内联map" class="headerlink" title="内联map"></a>内联map</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// evaluates to a Java map containing the two entries</span></span><br><span class="line">Map inventorInfo = (Map) parser.parseExpression(<span class="string">"&#123;name:'Nikola',dob:'10-July-1856'&#125;"</span>).getValue(context);</span><br><span class="line">Map mapOfMaps = (Map) parser.parseExpression(<span class="string">"&#123;name:&#123;first:'Nikola',last:'Tesla'&#125;,dob:&#123;day:10,month:'July',year:1856&#125;&#125;"</span>).getValue(context);</span><br></pre></td></tr></table></figure>
<h4 id="调用方法"><a href="#调用方法" class="headerlink" title="调用方法"></a>调用方法</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">boolean</span> isMember = parser.parseExpression(<span class="string">"isMember('Mihajlo Pupin')"</span>).getValue(</span><br><span class="line">        societyContext, Boolean.class);</span><br></pre></td></tr></table></figure>
<h4 id="运算符解析"><a href="#运算符解析" class="headerlink" title="运算符解析"></a>运算符解析</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">boolean</span> trueValue = parser.parseExpression(<span class="string">"'black' &lt; 'block'"</span>).getValue(Boolean.class);</span><br><span class="line"></span><br><span class="line"><span class="keyword">boolean</span> falseValue = parser.parseExpression(</span><br><span class="line">        <span class="string">"'xyz' instanceof T(Integer)"</span>).getValue(Boolean.class);</span><br><span class="line"></span><br><span class="line"><span class="comment">//正则支持</span></span><br><span class="line"><span class="comment">// evaluates to true</span></span><br><span class="line"><span class="keyword">boolean</span> trueValue = parser.parseExpression(</span><br><span class="line">        <span class="string">"'5.00' matches '\^-?\\d+(\\.\\d&#123;2&#125;)?$'"</span>).getValue(Boolean.class);</span><br><span class="line"></span><br><span class="line"><span class="comment">//evaluates to false</span></span><br><span class="line"><span class="keyword">boolean</span> falseValue = parser.parseExpression(</span><br><span class="line">        <span class="string">"'5.0067' matches '\^-?\\d+(\\.\\d&#123;2&#125;)?$'"</span>).getValue(Boolean.class);</span><br></pre></td></tr></table></figure>
<blockquote>
<p>注意null 在这里视为0 和 空  1&gt; null 返回true -1&gt;null 返回false</p>
</blockquote>
<blockquote>
<p>注意:每个符号运算符也可以被指定为纯粹的字母等效。这避免了所使用的符号对嵌入表达式的文档类型（例如XML文档）具有特殊含义的问题。文本等价物如下所示：lt（&lt;），gt（&gt;），le（⇐），ge（&gt;=），eq（==）， ne（!=），div（/），mod（%），not（!）</p>
</blockquote>
<h4 id="逻辑运算符"><a href="#逻辑运算符" class="headerlink" title="逻辑运算符"></a>逻辑运算符</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// -- AND --</span></span><br><span class="line"><span class="comment">// evaluates to false</span></span><br><span class="line"><span class="keyword">boolean</span> falseValue = parser.parseExpression(<span class="string">"true and false"</span>).getValue(Boolean.class);</span><br><span class="line"><span class="comment">// evaluates to true</span></span><br><span class="line">String expression = <span class="string">"isMember('Nikola Tesla') and isMember('Mihajlo Pupin')"</span>;</span><br><span class="line"><span class="keyword">boolean</span> trueValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class);</span><br><span class="line"><span class="comment">// -- OR --</span></span><br><span class="line"><span class="comment">// evaluates to true</span></span><br><span class="line"><span class="keyword">boolean</span> trueValue = parser.parseExpression(<span class="string">"true or false"</span>).getValue(Boolean.class);</span><br><span class="line"><span class="comment">// evaluates to true</span></span><br><span class="line">String expression = <span class="string">"isMember('Nikola Tesla') or isMember('Albert Einstein')"</span>;</span><br><span class="line"><span class="keyword">boolean</span> trueValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class);</span><br><span class="line"><span class="comment">// -- NOT --</span></span><br><span class="line"><span class="comment">// evaluates to false</span></span><br><span class="line"><span class="keyword">boolean</span> falseValue = parser.parseExpression(<span class="string">"!true"</span>).getValue(Boolean.class);</span><br><span class="line"><span class="comment">// -- AND and NOT --</span></span><br><span class="line">String expression = <span class="string">"isMember('Nikola Tesla') and !isMember('Mihajlo Pupin')"</span>;</span><br><span class="line"><span class="keyword">boolean</span> falseValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class);</span><br></pre></td></tr></table></figure>
<h4 id="数学运算符"><a href="#数学运算符" class="headerlink" title="数学运算符"></a>数学运算符</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Addition</span></span><br><span class="line"><span class="keyword">int</span> two = parser.parseExpression(<span class="string">"1 + 1"</span>).getValue(Integer.class); <span class="comment">// 2</span></span><br><span class="line"></span><br><span class="line">String testString = parser.parseExpression(</span><br><span class="line">        <span class="string">"'test' + ' ' + 'string'"</span>).getValue(String.class); <span class="comment">// 'test string'</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// Subtraction</span></span><br><span class="line"><span class="keyword">int</span> four = parser.parseExpression(<span class="string">"1 - -3"</span>).getValue(Integer.class); <span class="comment">// 4</span></span><br><span class="line"><span class="keyword">double</span> d = parser.parseExpression(<span class="string">"1000.00 - 1e4"</span>).getValue(Double.class); <span class="comment">// -9000</span></span><br><span class="line"><span class="comment">// Multiplication</span></span><br><span class="line"><span class="keyword">int</span> six = parser.parseExpression(<span class="string">"-2 * -3"</span>).getValue(Integer.class); <span class="comment">// 6</span></span><br><span class="line"><span class="keyword">double</span> twentyFour = parser.parseExpression(<span class="string">"2.0 * 3e0 * 4"</span>).getValue(Double.class); <span class="comment">// 24.0</span></span><br><span class="line"><span class="comment">// Division</span></span><br><span class="line"><span class="keyword">int</span> minusTwo = parser.parseExpression(<span class="string">"6 / -3"</span>).getValue(Integer.class); <span class="comment">// -2</span></span><br><span class="line"><span class="keyword">double</span> one = parser.parseExpression(<span class="string">"8.0 / 4e0 / 2"</span>).getValue(Double.class); <span class="comment">// 1.0</span></span><br><span class="line"><span class="comment">// Modulus</span></span><br><span class="line"><span class="keyword">int</span> three = parser.parseExpression(<span class="string">"7 % 4"</span>).getValue(Integer.class); <span class="comment">// 3</span></span><br><span class="line"><span class="keyword">int</span> one = parser.parseExpression(<span class="string">"8 / 5 % 2"</span>).getValue(Integer.class); <span class="comment">// 1</span></span><br><span class="line"><span class="comment">// Operator precedence</span></span><br><span class="line"><span class="keyword">int</span> minusTwentyOne = parser.parseExpression(<span class="string">"1+2-3*8"</span>).getValue(Integer.class); <span class="comment">// -21</span></span><br></pre></td></tr></table></figure>
<h4 id="属性赋值"><a href="#属性赋值" class="headerlink" title="属性赋值"></a>属性赋值</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">String aleks = parser.parseExpression(</span><br><span class="line">        <span class="string">"Name = 'Alexandar Seovic'"</span>).getValue(inventorContext, String.class);</span><br></pre></td></tr></table></figure>
<h4 id="构造函数"><a href="#构造函数" class="headerlink" title="构造函数"></a>构造函数</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Inventor einstein = p.parseExpression(</span><br><span class="line">        <span class="string">"new org.spring.samples.spel.inventor.Inventor('Albert Einstein', 'German')"</span>)</span><br><span class="line">        .getValue(Inventor.class);</span><br></pre></td></tr></table></figure>
<h4 id="三元运算符和猫王运算符"><a href="#三元运算符和猫王运算符" class="headerlink" title="三元运算符和猫王运算符"></a>三元运算符和猫王运算符</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">String falseString = parser.parseExpression(</span><br><span class="line">        <span class="string">"false ? 'trueExp' : 'falseExp'"</span>).getValue(String.class);</span><br><span class="line"></span><br><span class="line">String name = parser.parseExpression(<span class="string">"name?:'Unknown'"</span>).getValue(String.class);</span><br><span class="line"><span class="comment">//上面等价于</span></span><br><span class="line"></span><br><span class="line">String name = parser.parseExpression(<span class="string">"name==null?name:'Unknown'"</span>).getValue(String.class);</span><br></pre></td></tr></table></figure>
<h4 id="集合选择"><a href="#集合选择" class="headerlink" title="集合选择"></a>集合选择</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">List&lt;Inventor&gt; list = (List&lt;Inventor&gt;) parser.parseExpression(</span><br><span class="line">        <span class="string">"Members.?[Nationality == 'Serbian']"</span>).getValue(societyContext);</span><br><span class="line">Map newMap = parser.parseExpression(<span class="string">"map.?[value&lt;27]"</span>).getValue();</span><br></pre></td></tr></table></figure>
<p>着表达式将会选择 List Menbers中 Nationality 为 Serbian的所有元素</p>
<blockquote>
<p>引申:除了返回所有选定的元素之外，还可以检索第一个或最后一个值。要获得与选择相匹配的第一个条目，语法使用^[…​],要获取语法的最后匹配使用$[…​]。</p>
</blockquote>
<h4 id="在字符串中嵌入表达式模版"><a href="#在字符串中嵌入表达式模版" class="headerlink" title="在字符串中嵌入表达式模版"></a>在字符串中嵌入表达式模版</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">String randomPhrase = parser.parseExpression(</span><br><span class="line">        <span class="string">"random number is #&#123;T(java.lang.Math).random()&#125;"</span>,</span><br><span class="line">        <span class="keyword">new</span> TemplateParserContext()).getValue(String.class);</span><br></pre></td></tr></table></figure>
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/10/09/spring5-AOP/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="kyssion">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="kyssion-blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/10/09/spring5-AOP/" itemprop="url">未命名</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-10-09T15:22:37+08:00">
                2018-10-09
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <blockquote>
<p>AOP 是一种新的属性配置方法这种方法不同与OOP，不是使用继承的方法来增强相关的参数而是使用横向的切面编程的方法来进行相关增强的</p>
</blockquote>
<h3 id="spring中使用xml进行配置的方法"><a href="#spring中使用xml进行配置的方法" class="headerlink" title="spring中使用xml进行配置的方法"></a>spring中使用xml进行配置的方法</h3><p>切面类</p>
<blockquote>
<p>注意： around方式的方法需要第一参数为ProceedingJoinPoint 类型 ，当需要接受参数的时候需要在 exection方法中进行相关的配置</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> org.aspectj.lang.ProceedingJoinPoint;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CutClass</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">before</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"before: this is before"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">afterTwo</span><span class="params">(<span class="keyword">int</span> number, String string)</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">" this is after two "</span> + number + <span class="string">" "</span> + string);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">after</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"this is after"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Arround</span><span class="params">(ProceedingJoinPoint joinPoint)</span> <span class="keyword">throws</span> Throwable </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"this is start around"</span>);</span><br><span class="line">		Object[] objects = joinPoint.getArgs();</span><br><span class="line">		joinPoint.proceed(objects);</span><br><span class="line">		System.out.println(<span class="string">"this is after around"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">AfterReturn</span><span class="params">(<span class="keyword">int</span> number)</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"AfterReturn :"</span> + number);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">AfterThrow</span><span class="params">(Exception exception)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			System.out.println(<span class="string">"this is exception:"</span> + exception);</span><br><span class="line">		&#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">			<span class="comment">// <span class="doctag">TODO:</span> handle exception</span></span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>被切入类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BeCutClass</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">say</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"this is say"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">sayHasReturn</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="number">123</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">sayHasEx</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">int</span> a=<span class="number">1</span>/<span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">HasPress</span><span class="params">(<span class="keyword">int</span> number,String string)</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"this is sayHasOress"</span>+<span class="string">" number "</span>+number+<span class="string">" string:"</span>+string);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="xml配置文件"><a href="#xml配置文件" class="headerlink" title="xml配置文件"></a>xml配置文件</h5><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span><br><span class="line"><span class="tag">&lt;<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">"http://www.springframework.org/schema/beans"</span></span></span><br><span class="line"><span class="tag">	<span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></span></span><br><span class="line"><span class="tag">	<span class="attr">xmlns:aop</span>=<span class="string">"http://www.springframework.org/schema/aop"</span></span></span><br><span class="line"><span class="tag">	<span class="attr">xmlns:context</span>=<span class="string">"http://www.springframework.org/schema/context"</span></span></span><br><span class="line"><span class="tag">	<span class="attr">xsi:schemaLocation</span>=<span class="string">"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd</span></span></span><br><span class="line"><span class="tag"><span class="string">		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd</span></span></span><br><span class="line"><span class="tag"><span class="string">		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"cutpoint"</span>  <span class="attr">class</span>=<span class="string">"b_spring.c_springAOP.CutClass"</span>&gt;</span><span class="tag">&lt;/<span class="name">bean</span>&gt;</span>	</span><br><span class="line">	<span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"becutpoint"</span> <span class="attr">class</span>=<span class="string">"b_spring.c_springAOP.BeCutClass"</span>&gt;</span><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">aop:config</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">aop:pointcut</span> <span class="attr">expression</span>=<span class="string">"execution(* b_spring.c_springAOP.BeCutClass.say*(..) )"</span> <span class="attr">id</span>=<span class="string">"pointCurForNoPress"</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">aop:pointcut</span> <span class="attr">expression</span>=<span class="string">"execution(* b_spring.c_springAOP.BeCutClass.HasPress(..))"</span> <span class="attr">id</span>=<span class="string">"pointCurForPress"</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">aop:aspect</span> <span class="attr">ref</span>=<span class="string">"cutpoint"</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">aop:before</span> <span class="attr">method</span>=<span class="string">"before"</span> <span class="attr">pointcut-ref</span>=<span class="string">"pointCurForNoPress"</span>/&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">aop:after</span> <span class="attr">method</span>=<span class="string">"after"</span> <span class="attr">pointcut-ref</span>=<span class="string">"pointCurForNoPress"</span> /&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">aop:after-returning</span> <span class="attr">method</span>=<span class="string">"AfterReturn"</span> <span class="attr">pointcut-ref</span>=<span class="string">"pointCurForNoPress"</span> <span class="attr">returning</span>=<span class="string">"number"</span>/&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">aop:after-throwing</span> <span class="attr">method</span>=<span class="string">"AfterThrow"</span> <span class="attr">pointcut-ref</span>=<span class="string">"pointCurForNoPress"</span> <span class="attr">throwing</span>=<span class="string">"exception"</span>/&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">aop:around</span> <span class="attr">method</span>=<span class="string">"Arround"</span> <span class="attr">pointcut-ref</span>=<span class="string">"pointCurForPress"</span>&gt;</span><span class="tag">&lt;/<span class="name">aop:around</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">aop:after</span> <span class="attr">method</span>=<span class="string">"afterTwo"</span> <span class="attr">pointcut</span>=<span class="string">"execution(* b_spring.c_springAOP.BeCutClass.HasPress(..)) and args(number,string)"</span> <span class="attr">arg-names</span>=<span class="string">"number,string"</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">aop:aspect</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">aop:config</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h5 id="测试类"><a href="#测试类" class="headerlink" title="测试类"></a>测试类</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@org</span>.junit.Test</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test</span><span class="params">()</span> </span>&#123;</span><br><span class="line">	BeCutClass class1 = context.getBean(<span class="string">"becutpoint"</span>, BeCutClass.class);</span><br><span class="line">	class1.say();</span><br><span class="line">	class1.sayHasReturn();</span><br><span class="line">	class1.HasPress(<span class="number">123</span>, <span class="string">"jjj"</span>);</span><br><span class="line">	class1.sayHasEx();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="输出结果"><a href="#输出结果" class="headerlink" title="输出结果"></a>输出结果</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">this</span> is before</span><br><span class="line"><span class="keyword">this</span> is say</span><br><span class="line"><span class="keyword">this</span> is after</span><br><span class="line"><span class="keyword">this</span> is before</span><br><span class="line">AfterReturn :<span class="number">123</span></span><br><span class="line"><span class="keyword">this</span> is after</span><br><span class="line"><span class="keyword">this</span> is start around</span><br><span class="line"><span class="keyword">this</span> is sayHasOress number <span class="number">123</span> string:jjj</span><br><span class="line"><span class="keyword">this</span> is after two <span class="number">123</span> jjj</span><br><span class="line"><span class="keyword">this</span> is after around</span><br><span class="line"><span class="keyword">this</span> is before</span><br><span class="line"><span class="keyword">this</span> is exception:java.lang.ArithmeticException: / by zero</span><br><span class="line"><span class="keyword">this</span> is after</span><br></pre></td></tr></table></figure>
<h3 id="使用注解方法进行配置"><a href="#使用注解方法进行配置" class="headerlink" title="使用注解方法进行配置"></a>使用注解方法进行配置</h3><h4 id="增强类"><a href="#增强类" class="headerlink" title="增强类"></a>增强类</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Component;</span><br><span class="line"><span class="keyword">import</span> n_springAOP.noconfig.FatherBean;</span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AopBeanOne</span> <span class="keyword">implements</span> <span class="title">FatherBean</span></span>&#123;</span><br><span class="line">	<span class="comment">//--这里面 左面的小图标消失的启示-当实现接口的时候 spring框架自动使用jdk代理,没有使用接口的时候使用cglib代理</span></span><br><span class="line">	<span class="comment">//当使用使用了接口的时候aop增强的不是接口中的方法会导致spring的插件不识别但是spring的代码没有出现错误 说明是spring框架中使用的</span></span><br><span class="line">	<span class="comment">//了自动的判断方法</span></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">say</span><span class="params">(String one,String two)</span></span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"i'am aopbeanone"</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="string">"beanOneReturn"</span>+one+two;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="切面类"><a href="#切面类" class="headerlink" title="切面类"></a>切面类</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//定义切面类</span></span><br><span class="line"><span class="keyword">import</span> org.aspectj.lang.ProceedingJoinPoint;</span><br><span class="line"><span class="keyword">import</span> org.aspectj.lang.annotation.After;</span><br><span class="line"><span class="keyword">import</span> org.aspectj.lang.annotation.AfterReturning;</span><br><span class="line"><span class="keyword">import</span> org.aspectj.lang.annotation.AfterThrowing;</span><br><span class="line"><span class="keyword">import</span> org.aspectj.lang.annotation.Around;</span><br><span class="line"><span class="keyword">import</span> org.aspectj.lang.annotation.Aspect;</span><br><span class="line"><span class="keyword">import</span> org.aspectj.lang.annotation.Before;</span><br><span class="line"><span class="keyword">import</span> org.aspectj.lang.annotation.Pointcut;</span><br><span class="line"><span class="keyword">import</span> org.springframework.core.Ordered;</span><br><span class="line"><span class="keyword">import</span> org.springframework.core.annotation.Order;</span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Component;</span><br><span class="line"><span class="comment">//使用这个注解表明这个类是一个切面类 用来给其他的一些类进行增强处理</span></span><br><span class="line"><span class="comment">//spring的aop主要用来增强方法</span></span><br><span class="line"><span class="meta">@Component</span><span class="comment">//注意在不使用配置文件的时候要进行aop时候必须使用这条注解将所需要的方法放进spring容器中</span></span><br><span class="line"><span class="meta">@Aspect</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyAop</span> <span class="keyword">implements</span> <span class="title">Ordered</span></span>&#123;</span><br><span class="line">	<span class="comment">//aop增强表示在这个方法调用之前进行处理的过程</span></span><br><span class="line">	<span class="comment">//！！！注意在切入的点上还可以添加一个&amp;&amp;args(one,two)或者&amp;&amp;args(one,two,..)-前者表示匹配存在两个参数的方法并且讲参数传入进来-后一个表示匹配两个参数及其以上的方法</span></span><br><span class="line">	<span class="meta">@Before</span>(value=<span class="string">"execution(* n_springAOP.noconfig.aopbeanOne.*.*(..))&amp;&amp;args(one,two)"</span>)</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">methodForBefore</span><span class="params">(String one ,String two)</span></span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"i am is aop before"</span>+one+two);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//在aop增强的方法返回后进行的操作 returning 属性将会被动态的添加到Object参数中</span></span><br><span class="line">	<span class="comment">//这里使用了定义切入点方法 切人的函数在必要的时候可以是全限定名称</span></span><br><span class="line">	<span class="meta">@AfterReturning</span>(returning=<span class="string">"returning"</span>,pointcut=<span class="string">"myPintCut()"</span>)</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">methodForAfterReturning</span><span class="params">(Object returning)</span></span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"this is afterReturning return is "</span>+returning);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//这个增强表示在aop的方法抛出异常之后进行调用的方法  throwing 的属性将会动态的添加进ext参数中</span></span><br><span class="line">	<span class="meta">@AfterThrowing</span>(throwing=<span class="string">"ext"</span> ,value=<span class="string">"execution(* n_springAOP.noconfig.aopbeanOne.*.*(..))"</span>)</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">methodAfterThrowing</span><span class="params">(Throwable ext)</span></span>&#123;</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//这个增强表示在框架的执行过程中进行增强处理 之一使用Around 运行时增强的时候第一个参数必须是ProceedingJoinPoin类型</span></span><br><span class="line">	<span class="comment">//当调用ProceedingJoinPoint参数的proceed()方法的时候才会动态的调用需要增强的方法 里面的参数可以传递一个object[]的对象表示相关的调用方法的参数</span></span><br><span class="line">	<span class="comment">//这个参数存在一个getArgs方法 返回一个 object[] 表示调用时候传递进来的参数</span></span><br><span class="line">	<span class="comment">//这个增强可以出处理增强方法处理的时机 </span></span><br><span class="line">	<span class="meta">@Order</span>(<span class="number">1</span>)</span><br><span class="line">	<span class="meta">@Around</span>(value=<span class="string">"execution(* n_springAOP.noconfig.aopbeanOne.*.*(..))"</span>)</span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">methodForAround</span><span class="params">(ProceedingJoinPoint jp)</span> <span class="keyword">throws</span> Throwable</span>&#123;</span><br><span class="line">		Object[] oo=jp.getArgs();		<span class="comment">//返回调用的时候传递进来的参数</span></span><br><span class="line">		jp.getTarget();					<span class="comment">//返回进行aop增强的原始类</span></span><br><span class="line">		jp.getTarget();					<span class="comment">//返回进行aop增强的原始类</span></span><br><span class="line">		System.out.println(<span class="string">"This is Around begin...."</span>);</span><br><span class="line">		String a=(String) jp.proceed(oo);</span><br><span class="line">		System.out.println(<span class="string">"this is Around end....."</span>);</span><br><span class="line">		<span class="keyword">return</span> a;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//当多个切面作用在一个方法上的时候使用这个方法将会返回一优先级数值越小优先级高</span></span><br><span class="line">	<span class="comment">//除此之外还能使用注解定义优先级--使用继承接口的方法只是对全局进行定义的</span></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getOrder</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO Auto-generated method stub</span></span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@After</span>(<span class="string">"execution(* n_springAOP.noconfig.aopbeanOne.*.*(..))"</span>)</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">MethodAfter</span><span class="params">()</span></span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"this is after"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//切入点的使用相当的繁杂所以我们使用函数表示切入点 -详细情况见上面的afterReturning方法</span></span><br><span class="line">	<span class="meta">@Pointcut</span>(value=<span class="string">"execution(public * n_springAOP.noconfig.aopbeanOne.*.*(..))"</span>)</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">myPintCut</span><span class="params">()</span></span>&#123;&#125;</span><br><span class="line">	<span class="comment">//  execution(modifier-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)</span></span><br><span class="line">	<span class="comment">// modifier-pattern-指定方法的修饰符(public等) 支持通配符 可省略</span></span><br><span class="line">	<span class="comment">// ret-type-pattern -- 返回值类型 支持通配符</span></span><br><span class="line">	<span class="comment">//declaring-type-pattern- 方法所属的全限定类名称 可以使用通配可省略</span></span><br><span class="line">	<span class="comment">// name-pattern 方法的名称 可使用通配符*</span></span><br><span class="line">	<span class="comment">//(param-pattern) 表示接受的形参列表 可以使用 *-匹配一个参数 ..匹配多个参数 或者使用基本类型</span></span><br><span class="line">	<span class="comment">//表达式的扩展属性 	args 指定参数是指定类型的</span></span><br><span class="line">	<span class="comment">//					target 指定目标对象是指定类型的</span></span><br><span class="line">	<span class="comment">//主属性和扩展属性的链接符(连接不同的函数) &amp;&amp; 同时满足匹配 || 只满足一个或者多个匹配 ! 不满足匹配</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>切入点表达式 支持如下的几个,with(examples.chap03.Horseman) 指定类名称的所有方法,通过类名指定，同时包含所有子类target(examples.chap03.Horseman),args(java.util.String) 指定参数(如果使用的不是基本类型,那就会匹配对应的变量名称),annotation(org.springframework.transaction.annotation.Transactional) 指定标注了指定的注解的方法,这些参数必须是全限定名称</p>
</blockquote>
<h4 id="xml文件配置文件"><a href="#xml文件配置文件" class="headerlink" title="xml文件配置文件"></a>xml文件配置文件</h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span><br><span class="line"><span class="tag">&lt;<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">"http://www.springframework.org/schema/beans"</span></span></span><br><span class="line"><span class="tag">	<span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></span></span><br><span class="line"><span class="tag">	<span class="attr">xmlns:aop</span>=<span class="string">"http://www.springframework.org/schema/aop"</span></span></span><br><span class="line"><span class="tag">	<span class="attr">xmlns:context</span>=<span class="string">"http://www.springframework.org/schema/context"</span></span></span><br><span class="line"><span class="tag">	<span class="attr">xmlns:tx</span>=<span class="string">"http://www.springframework.org/schema/tx"</span></span></span><br><span class="line"><span class="tag">	<span class="attr">xsi:schemaLocation</span>=<span class="string">"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd</span></span></span><br><span class="line"><span class="tag"><span class="string">		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd</span></span></span><br><span class="line"><span class="tag"><span class="string">		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd</span></span></span><br><span class="line"><span class="tag"><span class="string">		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">context:component-scan</span> <span class="attr">base-package</span>=<span class="string">"n_springAOP.noconfig"</span>&gt;</span><span class="tag">&lt;/<span class="name">context:component-scan</span>&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- 不进行注册bean而是让注册了的bean进行相关的数据添加 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">context:annotation-config</span>&gt;</span><span class="tag">&lt;/<span class="name">context:annotation-config</span>&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- 这个标记表示启用注解形式的aop编程风格 --&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- 注意使用aop的时候需要添加 aspectj的库 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">aop:aspectj-autoproxy</span>&gt;</span><span class="tag">&lt;/<span class="name">aop:aspectj-autoproxy</span>&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- 如果不使用上面的aop配置文件需要启用这个bean后置处理器来为spring 框架中的所有bean --&gt;</span></span><br><span class="line">	<span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">		&lt;bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"&gt;&lt;/bean&gt;</span></span><br><span class="line"><span class="comment">    --&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>注意上面的配置方法其实可以替换成如下的方法</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configurable</span> <span class="comment">//使用注解模式</span></span><br><span class="line"><span class="meta">@ComponentScan</span> <span class="comment">//自动扫描路径</span></span><br><span class="line"><span class="meta">@EnableAspectJAutoProxy</span>  <span class="comment">// 启动aop支持</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ConfigApp</span></span>&#123;</span><br><span class="line">	............</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>注意:完整的AspectJ切入点语言支持Spring中不支持的其他切入点指示符。这些是：call, get, set, preinitialization, staticinitialization, initialization, handler, adviceexecution, withincode, cflow, cflowbelow, if, @this，和@withincode</p>
</blockquote>
<blockquote>
<p>注意: 使用注解的缺点,支持“singleton”方面的实例化模型，并且不可能组合使用XML声明的命名切入点。使用XML方法的缺点是您无法通过组合这些定义来定义切入点。</p>
</blockquote>
<blockquote>
<p>注意: spring不支持final的调用,应为final不支持覆盖</p>
</blockquote>
<blockquote>
<p>注意当在代理中使用方法的方法的时候不能使用代理自己的方法,因为解析器会使用this.xxx()来运行 而不是使用 proxy来运行,所以应该使用如下的方法进行代理</p>
</blockquote>
<blockquote>
<p>注意: 使用的表达式格式 - execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-pattern(param-pattern) throws-pattern?)</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SimplePojo</span> <span class="keyword">implements</span> <span class="title">Pojo</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">foo</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// this works, but... gah!</span></span><br><span class="line">        ((Pojo) AopContext.currentProxy()).bar();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">bar</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// some logic...</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/10/09/spring5-springMVC异步请求支持/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="kyssion">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="kyssion-blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/10/09/spring5-springMVC异步请求支持/" itemprop="url">未命名</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-10-09T15:22:37+08:00">
                2018-10-09
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="pring5-springMVC异步请求支持"><a href="#pring5-springMVC异步请求支持" class="headerlink" title="pring5-springMVC异步请求支持"></a>pring5-springMVC异步请求支持</h2><p>提供AsyncRestTemplate用于客户端非阻塞异步支持</p>
<h3 id="服务端"><a href="#服务端" class="headerlink" title="服务端"></a>服务端</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserController</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> UserService userService;</span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">UserController</span><span class="params">(UserService userService)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.userService = userService;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">@RequestMapping</span>(<span class="string">"/api"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> Callable&lt;User&gt; <span class="title">api</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"=====hello"</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> Callable&lt;User&gt;() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> User <span class="title">call</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">                Thread.sleep(<span class="number">10L</span> * <span class="number">1000</span>); <span class="comment">//暂停两秒</span></span><br><span class="line">                User user = <span class="keyword">new</span> User();</span><br><span class="line">                user.setId(<span class="number">1L</span>);</span><br><span class="line">                user.setName(<span class="string">"haha"</span>);</span><br><span class="line">                <span class="keyword">return</span> user;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="客户端"><a href="#客户端" class="headerlink" title="客户端"></a>客户端</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserController</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        AsyncRestTemplate template = <span class="keyword">new</span> AsyncRestTemplate();</span><br><span class="line">        <span class="comment">//调用完后立即返回（没有阻塞）</span></span><br><span class="line">        ListenableFuture&lt;ResponseEntity&lt;User&gt;&gt; future = template.getForEntity(<span class="string">"http://localhost:9080/spring4/api"</span>, User.class);</span><br><span class="line">        <span class="comment">//设置异步回调</span></span><br><span class="line">        future.addCallback(<span class="keyword">new</span> ListenableFutureCallback&lt;ResponseEntity&lt;User&gt;&gt;() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onSuccess</span><span class="params">(ResponseEntity&lt;User&gt; result)</span> </span>&#123;</span><br><span class="line">                System.out.println(<span class="string">"======client get result : "</span> + result.getBody());</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onFailure</span><span class="params">(Throwable t)</span> </span>&#123;</span><br><span class="line">                System.out.println(<span class="string">"======client failure : "</span> + t);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">        System.out.println(<span class="string">"==no wait"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>注意</strong>:这个方法在spring5中是过期的，使用 WebClient</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/10/09/javaEE servlet3.1规范/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="kyssion">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="kyssion-blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/10/09/javaEE servlet3.1规范/" itemprop="url">未命名</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-10-09T15:22:37+08:00">
                2018-10-09
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>说道spring的web支持首先就可以想到了spring MVC 的技术(其他的技术还有spring的webflux 以后讨论),从这片博客开始要进行相关知识点的整理.</p>
<h3 id="spring-MVC-整体的架构设计"><a href="#spring-MVC-整体的架构设计" class="headerlink" title="spring MVC 整体的架构设计"></a>spring MVC 整体的架构设计</h3><p><img src="/blogimg/springMVC/1.jpg" alt=""></p>
<p>spring 自己的webapplication支持嵌套作用域,通过这个方法可以实现spring applicationcongtext的继承特性(继承特性,子作用域可以访问夫作用域的中的属性,但是父作用域中的属性无法访问子作用域中的属性,具体的使用看HierarchicalBeanFactory)</p>
<h4 id="web容器的初始化设置"><a href="#web容器的初始化设置" class="headerlink" title="web容器的初始化设置"></a>web容器的初始化设置</h4><p>springMVC 支持使用xml进行配置</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">web-app</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">listener</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">listener-class</span>&gt;</span>org.springframework.web.context.ContextLoaderListener<span class="tag">&lt;/<span class="name">listener-class</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">listener</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">context-param</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">param-name</span>&gt;</span>contextConfigLocation<span class="tag">&lt;/<span class="name">param-name</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">param-value</span>&gt;</span>/WEB-INF/app-context.xml<span class="tag">&lt;/<span class="name">param-value</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">context-param</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">servlet</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>app<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-class</span>&gt;</span>org.springframework.web.servlet.DispatcherServlet<span class="tag">&lt;/<span class="name">servlet-class</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">init-param</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">param-name</span>&gt;</span>contextConfigLocation<span class="tag">&lt;/<span class="name">param-name</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">param-value</span>&gt;</span><span class="tag">&lt;/<span class="name">param-value</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">init-param</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">load-on-startup</span>&gt;</span>1<span class="tag">&lt;/<span class="name">load-on-startup</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">servlet</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">servlet-mapping</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>app<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">url-pattern</span>&gt;</span>/app/*<span class="tag">&lt;/<span class="name">url-pattern</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">servlet-mapping</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">web-app</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>从spring5.0 开始 spring官方文档提倡使用接口配置,容器在初始化的时候将会自动的加载这个接口的实现类从而进行配置<br>spring mvc 的自动化配置是通过 WebApplicationInitializer 接口实现的 但是系统提供了更加高级的接口 AbstractAnnotationConfigDispatcherServletInitializer </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyWebAppInitializer</span> <span class="keyword">extends</span> <span class="title">AbstractAnnotationConfigDispatcherServletInitializer</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">protected</span> Class&lt;?&gt;[] getRootConfigClasses() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> Class&lt;?[] &#123; RootConfig.class &#125;;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">protected</span> Class&lt;?&gt;[] getServletConfigClasses() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> Class&lt;?[] &#123; App1Config.class &#125;;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">protected</span> String[] getServletMappings() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> String[] &#123; <span class="string">"/app1/*"</span> &#125;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> org.springframework.web.WebApplicationInitializer;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyWebApplicationInitializer</span> <span class="keyword">implements</span> <span class="title">WebApplicationInitializer</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onStartup</span><span class="params">(ServletContext container)</span> </span>&#123;</span><br><span class="line">        XmlWebApplicationContext appContext = <span class="keyword">new</span> XmlWebApplicationContext();</span><br><span class="line">        appContext.setConfigLocation(<span class="string">"/WEB-INF/spring/dispatcher-config.xml"</span>);</span><br><span class="line"></span><br><span class="line">        ServletRegistration.Dynamic registration = container.addServlet(<span class="string">"dispatcher"</span>, <span class="keyword">new</span> DispatcherServlet(appContext));</span><br><span class="line">        registration.setLoadOnStartup(<span class="number">1</span>);</span><br><span class="line">        registration.addMapping(<span class="string">"/"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>如果使用基于xml 的 spring配置则需要使用这个方法进行相关的调用</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyWebAppInitializer</span> <span class="keyword">extends</span> <span class="title">AbstractDispatcherServletInitializer</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> WebApplicationContext <span class="title">createRootApplicationContext</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> WebApplicationContext <span class="title">createServletApplicationContext</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        XmlWebApplicationContext cxt = <span class="keyword">new</span> XmlWebApplicationContext();</span><br><span class="line">        cxt.setConfigLocation(<span class="string">"/WEB-INF/spring/dispatcher-config.xml"</span>);</span><br><span class="line">        <span class="keyword">return</span> cxt;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">protected</span> String[] getServletMappings() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> String[] &#123; <span class="string">"/"</span> &#125;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>如果要添加filter 配置 重构如下的方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyWebAppInitializer</span> <span class="keyword">extends</span> <span class="title">AbstractDispatcherServletInitializer</span> </span>&#123;</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">protected</span> Filter[] getServletFilters() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> Filter[] &#123;</span><br><span class="line">            <span class="keyword">new</span> HiddenHttpMethodFilter(), <span class="keyword">new</span> CharacterEncodingFilter() &#125;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这个方法将会为每一个filter 添加一个默认的过滤器,并且自动的添加到对应的display中</p>
<p>这个方法还有一个isisAsyncSupported() 默认情况下返回true 表示spring mvc 框架中的filter 将会异步的处理请求</p>
<blockquote>
<p>当使用idea 进行操作的时候注意要保证spring—web包要在class path目录下，不然tomcat等web 容器将不会使用spi技术将相关的各种需要的东西夹在到class中</p>
</blockquote>
<p>引申： tomcat 此处实现spi技术的解析</p>
<p>spring的web包的META-INF的文件夹中有一个名为，javax.servlet.ServletContainerInitializer的文件，其中的内容org.springframework.web.SpringServletContainerInitializer，表示定义的ServletContainerInitializer和spring的实现接口SpringServletContainerInitializer，其中@HandlesTypes注解表示CustomServletContainerInitializer 可以处理的类，在onStartup 方法中，可以通过Set&lt;Class&lt;?&gt;&gt; c 获取得到。</p>
<p>见下面代码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@HandlesTypes</span>(WebApplicationInitializer.class)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SpringServletContainerInitializer</span> <span class="keyword">implements</span> <span class="title">ServletContainerInitializer</span> </span>&#123;</span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onStartup</span><span class="params">(@Nullable Set&lt;Class&lt;?&gt;&gt; webAppInitializerClasses, ServletContext servletContext)</span></span></span><br><span class="line"><span class="function">			<span class="keyword">throws</span> ServletException </span>&#123;</span><br><span class="line">		List&lt;WebApplicationInitializer&gt; initializers = <span class="keyword">new</span> LinkedList&lt;&gt;()</span><br><span class="line">		<span class="keyword">if</span> (webAppInitializerClasses != <span class="keyword">null</span>) &#123;</span><br><span class="line">			<span class="keyword">for</span> (Class&lt;?&gt; waiClass : webAppInitializerClasses) &#123;</span><br><span class="line">				<span class="keyword">if</span> (!waiClass.isInterface() &amp;&amp; !Modifier.isAbstract(waiClass.getModifiers()) &amp;&amp;</span><br><span class="line">						WebApplicationInitializer.class.isAssignableFrom(waiClass)) &#123;</span><br><span class="line">					<span class="keyword">try</span> &#123;</span><br><span class="line">						initializers.add((WebApplicationInitializer)</span><br><span class="line">								ReflectionUtils.accessibleConstructor(waiClass).newInstance());</span><br><span class="line">					&#125;</span><br><span class="line">					<span class="keyword">catch</span> (Throwable ex) &#123;</span><br><span class="line">						<span class="keyword">throw</span> <span class="keyword">new</span> ServletException(<span class="string">"Failed to instantiate WebApplicationInitializer class"</span>, ex);</span><br><span class="line">					&#125;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span> (initializers.isEmpty()) &#123;</span><br><span class="line">			servletContext.log(<span class="string">"No Spring WebApplicationInitializer types detected on classpath"</span>);</span><br><span class="line">			<span class="keyword">return</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		servletContext.log(initializers.size() + <span class="string">" Spring WebApplicationInitializers detected on classpath"</span>);</span><br><span class="line">		AnnotationAwareOrderComparator.sort(initializers);</span><br><span class="line">		<span class="keyword">for</span> (WebApplicationInitializer initializer : initializers) &#123;</span><br><span class="line">			initializer.onStartup(servletContext);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="最核心类-DispatcherServlet"><a href="#最核心类-DispatcherServlet" class="headerlink" title="最核心类 DispatcherServlet"></a>最核心类 DispatcherServlet</h3><p>如果要说这个类就需要看一下springmvc的流程图</p>
<p><img src="/blogimg/1.png" alt="">;</p>
<p>在这里之前 DispathcerServlet 将会webapplicationcontext的字符串引用放入java中 </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> String WEB_APPLICATION_CONTEXT_ATTRIBUTE = DispatcherServlet.class.getName() + <span class="string">".CONTEXT"</span>;</span><br></pre></td></tr></table></figure>
<p>一个一个看</p>
<ol>
<li>HandlerMapping </li>
</ol>
<p>这个类解决了 url地址映射到对应的处理类中，主要有两个实现RequestMappingHandlerMapping-为@RequestMapping 注解提供支持 ，SimpleUrlHandlerMapping，实现简单的url地址映射</p>
<ol start="2">
<li>HandlerExceptionResolver </li>
</ol>
<p>这个是试图返回的异常处理包括相关的错误处理方法</p>
<ol start="3">
<li>HandlerIntercepter </li>
</ol>
<p>处理相关的接口进行拦截 </p>
<ol start="4">
<li>HandlerAdapter</li>
</ol>
<p>使用适配器模式，将试图的映射由指定的接口处理</p>
<ol start="5">
<li>各种resolver 提供视图解析展示的功能</li>
</ol>
<p>LocaleResolver, LocaleContextResolver，ThemeResolver，MultipartResolver</p>
<ol start="6">
<li>FlashMapManager</li>
</ol>
<p>处理flash的时候使用的 估计用不到了</p>
<h3 id="Interception-拦截器"><a href="#Interception-拦截器" class="headerlink" title="Interception 拦截器"></a>Interception 拦截器</h3><p>在spring MVC 中声明springmvc 的方法有如下几种：</p>
<ol>
<li>实现HandlerInterceptor接口或者实现HandlerInterceptorAdapter 抽象类</li>
<li>实现WebRequestInterceptor接口，或者实现了WebRequestInterceptor的类</li>
</ol>
<h4 id="HandlerInterceptor-接口方法"><a href="#HandlerInterceptor-接口方法" class="headerlink" title="HandlerInterceptor 接口方法"></a>HandlerInterceptor 接口方法</h4><p>(1)boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handle)方法，这个将会在请求传入之前进行相关的处理，如果返回值是true，将会自动的进行链式调用其他按照顺序执行的定时器，否则将会终止调用controll和其他拦截器。</p>
<p>(2) postHandle (HttpServletRequest request, HttpServletResponse response, Object handle, ModelAndView modelAndView) 方法,这个方法将会在处理器执行完后进行处理，和preHandle的执行方法，注意这个方法将不会自动拦截@requsetBody注解ResponseEntity</p>
<p>注意这里：（1）和（2）的第三个参数 handle 官方的解释是@controller标记的本身或者使用HandleMethod这个类，其实这里是springmvc 自己增强的方法，</p>
<blockquote>
<p>ResponseEntity 详解： 这个类其实是对 http请求的一个封装，封装了http的报头，状态码，http code 等数据，本质上是一种通信协议</p>
</blockquote>
<p>如果使用的不是string类型，那么将会是一种类似rpc协议</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//客户端</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">client</span><span class="params">()</span></span>&#123;</span><br><span class="line">    RestTemplate restTemplate = <span class="keyword">new</span> RestTemplate();</span><br><span class="line">    ResponseEntity&lt;String&gt; entity = restTemplate.getForEntity(<span class="string">"http://example.com"</span>, String.class);</span><br><span class="line">    String body = entity.getBody();</span><br><span class="line">    MediaType contentType = entity.getHeaders().getContentType();</span><br><span class="line">    HttpStatus statusCode = entity.getStatusCode();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//服务端</span></span><br><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">"/handle"</span>)</span><br><span class="line"><span class="function"><span class="keyword">public</span> ResponseEntity&lt;String&gt; <span class="title">handle</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    URI location = ...;</span><br><span class="line">    HttpHeaders responseHeaders = <span class="keyword">new</span> HttpHeaders();</span><br><span class="line">    responseHeaders.setLocation(location);</span><br><span class="line">    responseHeaders.set(<span class="string">"MyResponseHeader"</span>, <span class="string">"MyValue"</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> ResponseEntity&lt;String&gt;(<span class="string">"Hello World"</span>, responseHeaders,   HttpStatus.CREATED);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>当使用string类型的时候就和@ResponceBody+@ResponseStatus 相同</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">"/handle"</span>)</span><br><span class="line"><span class="function"><span class="keyword">public</span> ResponseEntity&lt;String&gt; <span class="title">handle</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    URI location = ...;</span><br><span class="line">    <span class="keyword">return</span> ResponseEntity.created(location).header(<span class="string">"MyResponseHeader"</span>,  <span class="string">"MyValue"</span>).body(<span class="string">"Hello World"</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>(3)afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handle, Exception ex) 方法，该方法也是需要当前对应的Interceptor 的preHandle 方法的返回值为true 时才会执行。该方法将在整个请求结束之后，也就是在DispatcherServlet 渲染了对应的视图之后执行。这个方法的主要作用是用于进行资源清理工作的。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyHandlerInterceptor</span> <span class="keyword">implements</span> <span class="title">HandlerInterceptor</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">preHandle</span><span class="params">(HttpServletRequest request, HttpServletResponse response, Object handler)</span> <span class="keyword">throws</span> Exception </span>&#123; <span class="keyword">return</span> <span class="keyword">false</span>; &#125;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">postHandle</span><span class="params">(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)</span> <span class="keyword">throws</span> Exception </span>&#123; &#125;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">afterCompletion</span><span class="params">(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)</span> <span class="keyword">throws</span> Exception </span>&#123; &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>除了实现HandlerInterceptor可以实现拦截器之外，使用WebRequestInterceptor 同样可以实现拦截器</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyWebHandlerInterceptor</span> <span class="keyword">implements</span> <span class="title">WebRequestInterceptor</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">preHandle</span><span class="params">(WebRequest request)</span> <span class="keyword">throws</span> Exception </span>&#123;&#125;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">postHandle</span><span class="params">(WebRequest request, ModelMap model)</span> <span class="keyword">throws</span> Exception </span>&#123;&#125;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">afterCompletion</span><span class="params">(WebRequest request, Exception ex)</span> <span class="keyword">throws</span> Exception </span>&#123;&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>(1)preHandle(WebRequest request) 方法。注意这个方法没有返回直，一般主要用它来进行资源的准备工作，其中的webrequest参数是HttpServletRequest的加强版，可以使用setAttribute(name, value, scope)添加参数到指定的作用域中，scope参数有如下的几个值：</p>
<pre><code>1. SCOPE_REQUEST ：它的值是0 ，代表只有在request 中可以访问。
2. SCOPE_SESSION ：它的值是1 ，如果环境允许的话它代表的是一个局部的隔离的session，否则就代表普通的session，并且在该session范围内可以访问。
3. SCOPE_GLOBAL_SESSION ：它的值是2 ，如果环境允许的话，它代表的是一个全局共享的session，否则就代表普通的session，并且在该session 范围内可以访问。
</code></pre><p>(2)postHandle(WebRequest request, ModelMap model) 方法。该方法将在请求处理之后，也就是在Controller 方法调用之后被调用，但是会在视图返回被渲染之前被调用，request 就是传递的请求参数，model就是返回的视图<br>(3)afterCompletion(WebRequest request, Exception ex) 方法。该方法会在整个请求处理完成，也就是在视图返回并被渲染之后执行。所以在该方法中可以进行资源的释放操作。而WebRequest 参数就可以把我们在preHandle 中准备的资源传递到这里进行释放。Exception 参数表示的是当前请求的异常对象，如果在Controller 中抛出的异常已经被Spring 的异常处理器给处理了的话，那么这个异常对象就是是null 。</p>
<p>####HandlerExceptionResolver springmvc 异常处理</p>
<p>异常处理用于处理@controll 这种接口抛出的各种异常，主要有如下的几种  </p>
<ol>
<li><p>SimpleMappingExceptionResolver 异常类名称和错误视图名称之间的映射。用于在浏览器应用程序中呈现错误页面。</p>
</li>
<li><p>DefaultHandlerExceptionResolver 解决Spring MVC引发的异常并将它们映射到HTTP状态代码。另请参阅备用ResponseEntityExceptionHandler和REST API例外。</p>
</li>
<li><p>ResponseStatusExceptionResolver 根据@ResponseStatus注释中的值解决注释中的异常并将其映射到HTTP状态代码。</p>
</li>
<li><p>ExceptionHandlerExceptionResolver 通过调用@ExceptionHandler一个@Controller或一个 @ControllerAdvice类中的方法来解决异常。请参阅@ExceptionHandler方法。</p>
</li>
</ol>
<p>对于异常处理来说通常的解决结果有如下的几种方法</p>
<ol>
<li><p>使用ModelAndView 指向错误视图。</p>
</li>
<li><p>返回空的modelandview 如果异常被处理</p>
</li>
<li><p>如果异常没有被解决，将会使用异常调用连进行处理，如果调用到最后将会抛出到servlet中</p>
</li>
</ol>
<p>在spring mvc 中集中进行异常处理的有三种方法</p>
<ol>
<li>使用@ResponseStatus 注释一个异常类，当spring中抛出这个异常的时候将会自动的交由这个类处理,并且可以制定http code值，比如下面的方法将会跑出403错误</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zj.exception;</span><br><span class="line"><span class="keyword">import</span> org.springframework.http.HttpStatus;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.ResponseStatus;</span><br><span class="line"><span class="meta">@ResponseStatus</span>(value=HttpStatus.FORBIDDEN,reason=<span class="string">"用户不匹配"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserNotMatchException</span> <span class="keyword">extends</span> <span class="title">RuntimeException</span></span>&#123;&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">"/testResponseStatus"</span>)</span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">testResponseStatus</span><span class="params">(<span class="keyword">int</span> i)</span></span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(i==<span class="number">0</span>)</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> UserNotMatchException();</span><br><span class="line">    <span class="keyword">return</span> <span class="string">"hello"</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>注意如果这个注解使用在 一个方法上的时候 ， 不论结果如何都将会放回制定的httpcode 异常</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@ResponseStatus</span>(value=HttpStatus.FORBIDDEN,reason=<span class="string">"用户名不匹配"</span>)</span><br><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">"/testResponseStatus"</span>)</span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">testResponseStatus</span><span class="params">(<span class="keyword">int</span> i)</span></span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(i==<span class="number">0</span>)</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> UserNotMatchException();</span><br><span class="line">    <span class="keyword">return</span> <span class="string">"hello"</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>2.@ControllerAdvice 和 @ExceptionHandler</p>
<p>这两个注解同样用于异常处理</p>
<p>（1）@ExceptionHandler 当一个Controller中有方法加了@ExceptionHandler之后，这个Controller其他方法中没有捕获的异常就会以参数的形式传入加了@ExceptionHandler注解的那个方法中。<strong>注意这个方法要有一个参数，这个参数就是指定要处理的异常</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Created by liuruijie.</span></span><br><span class="line"><span class="comment"> * 处理异常的类，需要处理异常的Controller直接继承这个类</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BaseController</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 处理Controller抛出的异常</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> e 异常实例</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> Controller层的返回值</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@ExceptionHandler</span></span><br><span class="line">    <span class="meta">@ResponseBody</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">expHandler</span><span class="params">(Exception e)</span></span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(e <span class="keyword">instanceof</span> SystemException)&#123;</span><br><span class="line">            SystemException ex= (SystemException) e;</span><br><span class="line">            <span class="keyword">return</span> WebResult.buildResult().status(ex.getCode())</span><br><span class="line">                            .msg(ex.getMessage());</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">            <span class="keyword">return</span> WebResult.buildResult().status(Config.FAIL)</span><br><span class="line">                            .msg(<span class="string">"系统错误"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>（2）@ControllerAdvice  实现这个注解的类可以让这个类中 @ExceptionHandler标记的方法实现全觉异常监听</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@ControllerAdvice</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">GlobalExceptionHandler</span> </span>&#123;</span><br><span class="line">   <span class="comment">//处理自定义的异常</span></span><br><span class="line">   <span class="meta">@ExceptionHandler</span>(SystemException.class) </span><br><span class="line">   <span class="meta">@ResponseBody</span></span><br><span class="line">   <span class="function"><span class="keyword">public</span> Object <span class="title">customHandler</span><span class="params">(SystemException e)</span></span>&#123;</span><br><span class="line">      e.printStackTrace();</span><br><span class="line">      <span class="keyword">return</span> WebResult.buildResult().status(e.getCode()).msg(e.getMessage());</span><br><span class="line">   &#125;</span><br><span class="line">   <span class="comment">//其他未处理的异常</span></span><br><span class="line">   <span class="meta">@ExceptionHandler</span>(Exception.class)</span><br><span class="line">   <span class="meta">@ResponseBody</span></span><br><span class="line">   <span class="function"><span class="keyword">public</span> Object <span class="title">exceptionHandler</span><span class="params">(Exception e)</span></span>&#123;</span><br><span class="line">      e.printStackTrace();</span><br><span class="line">      <span class="keyword">return</span> WebResult.buildResult().status(Config.FAIL).msg(<span class="string">"系统错误"</span>);</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>最后对于springmvc 如果异常没有被处理，spring提供了默认的页面展示异常，使用如下方法配置<br>xml ： 制定默认错误页面<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">error-page</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">location</span>&gt;</span>/error<span class="tag">&lt;/<span class="name">location</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">error-page</span>&gt;</span></span><br></pre></td></tr></table></figure></p>
<p> java ： 处理url<br> <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ErrorController</span> </span>&#123;</span><br><span class="line">    <span class="meta">@RequestMapping</span>(path = <span class="string">"/error"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> Map&lt;String, Object&gt; <span class="title">handle</span><span class="params">(HttpServletRequest request)</span> </span>&#123;</span><br><span class="line">        Map&lt;String, Object&gt; map = <span class="keyword">new</span> HashMap&lt;String, Object&gt;();</span><br><span class="line">        map.put(<span class="string">"status"</span>, request.getAttribute(<span class="string">"javax.servlet.error.status_code"</span>));</span><br><span class="line">        map.put(<span class="string">"reason"</span>, request.getAttribute(<span class="string">"javax.servlet.error.message"</span>));</span><br><span class="line">        <span class="keyword">return</span> map;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h4 id="ViewResolver-提供了view-名称到view实例之间的绑定，并且完成真正视图展示之前将相关数据进行整理的功能"><a href="#ViewResolver-提供了view-名称到view实例之间的绑定，并且完成真正视图展示之前将相关数据进行整理的功能" class="headerlink" title="ViewResolver 提供了view 名称到view实例之间的绑定，并且完成真正视图展示之前将相关数据进行整理的功能"></a>ViewResolver 提供了view 名称到view实例之间的绑定，并且完成真正视图展示之前将相关数据进行整理的功能</h4><p>spring mvc 中将相关的视图变成一个网页经历的过程</p>
<ol>
<li>将SpringMVC控制器中的返回结果封装成一个ModelAndView对象。</li>
<li>通过SpringMVC中的视图解析器，使用ViewResolver对控制器返回的ModelAndView对象进行解析，将逻辑视图转换成物理视图。</li>
<li>调用View中的render()方法对物理视图进行渲染。</li>
</ol>
<p>几个主要的视图的介绍</p>
<ol>
<li>AbstractCachingViewResolver: 最抽象的类提供了视图缓存的功能</li>
<li>UrlBasedViewResolver: 提供了更加细粒度的url控制，支持前缀后缀等功能，但是使用这个方法必须制定相关的view解析工具，默认使用的是jsp解析工具InternalResourceView（和可以使用其他的是解析工具比如FreeMarkerView等）， 并且提供了重定向和转发的功能</li>
</ol>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">bean</span>  </span></span><br><span class="line"><span class="tag">   <span class="attr">class</span>=<span class="string">"org.springframework.web.servlet.view.UrlBasedViewResolver"</span>&gt;</span>  </span><br><span class="line">   <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"prefix"</span> <span class="attr">value</span>=<span class="string">"/WEB-INF/"</span> /&gt;</span>  </span><br><span class="line">   <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"suffix"</span> <span class="attr">value</span>=<span class="string">".jsp"</span> /&gt;</span>  </span><br><span class="line">   <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"viewClass"</span> <span class="attr">value</span>=<span class="string">"org.springframework.web.servlet.view.InternalResourceView"</span>/&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br></pre></td></tr></table></figure>
<ol start="3">
<li>InternalResourceViewResolver：这个方法是 UrlBasedViewResolver 的子类，支持父类的所有功能，InternalResourceViewResolver会把返回的视图名称都解析为InternalResourceView对象，内部使用重定向的方法，将controller返回的view 包装成InternalResourceView， 并且鞋带上前缀和后缀，同时再转发出去</li>
</ol>
<p>视图解析链：<br>在SpringMVC中可以同时定义多个ViewResolver视图解析器，然后它们会组成一个ViewResolver链。当Controller处理器方法返回一个逻辑视图名称后，ViewResolver链将根据其中ViewResolver的优先级来进行处理。所有的ViewResolver都实现了Ordered接口，在Spring中实现了这个接口的类都是可以排序的。在ViewResolver中是通过order属性来指定顺序的，默认都是最大值。所以我们可以通过指定ViewResolver的order属性来实现ViewResolver的优先级，order属性是Integer类型，order越小，对应的ViewResolver将有越高的解析视图的权利，所以第一个进行解析的将是ViewResolver链中order值最小的那个。当一个ViewResolver在进行视图解析后返回的View对象是null的话就表示该ViewResolver不能解析该视图，这个时候如果还存在其他order值比它大的ViewResolver就会调用剩余的ViewResolver中的order值最小的那个来解析该视图，依此类推。当ViewResolver在进行视图解析后返回的是一个非空的View对象的时候，就表示该ViewResolver能够解析该视图，那么视图解析这一步就完成了，后续的ViewResolver将不会再用来解析该视图。当定义的所有ViewResolver都不能解析该视图的时候，Spring就会抛出一个异常。</p>
<h3 id="spring-mvc-常用注解"><a href="#spring-mvc-常用注解" class="headerlink" title="spring mvc 常用注解"></a>spring mvc 常用注解</h3><p>spring 提供了一整套注解来简化spring相关的配置</p>
<h4 id="Controll-和-RestController"><a href="#Controll-和-RestController" class="headerlink" title="@Controll 和 @RestController"></a>@Controll 和 @RestController</h4><p>Spring MVC提供了一种基于注释的编程模型，其中@Controller和@RestController组件使用注释来表示请求映射，请求输入，异常处理等。</p>
<p>其中的@RestController 是@ResponseBody和@Controller注解的一种集合。<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Target</span>(ElementType.TYPE)</span><br><span class="line"><span class="meta">@Retention</span>(RetentionPolicy.RUNTIME)</span><br><span class="line"><span class="meta">@Documented</span></span><br><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="meta">@ResponseBody</span></span><br><span class="line"><span class="keyword">public</span> <span class="meta">@interface</span> RestController &#123;</span><br><span class="line">	<span class="meta">@AliasFor</span>(annotation = Controller.class)</span><br><span class="line">	<span class="function">String <span class="title">value</span><span class="params">()</span> <span class="keyword">default</span> ""</span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>注意：如果使用aop方法对controller 进行增强的话请使用class-based proxying代理，但是如果使用了非spring context回调接口的方法的时候，需要明确的制定相关的配置信息：<a href="tx:annotation-driven/" target="_blank" rel="noopener">tx:annotation-driven/</a>, 改变为 &lt;tx:annotation-driven proxy-target-class=”true”/&gt;.</p>
<h4 id="RequestMapping"><a href="#RequestMapping" class="headerlink" title="@RequestMapping"></a>@RequestMapping</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Target</span>(&#123;ElementType.METHOD, ElementType.TYPE&#125;)</span><br><span class="line"><span class="meta">@Retention</span>(RetentionPolicy.RUNTIME)</span><br><span class="line"><span class="meta">@Documented</span></span><br><span class="line"><span class="meta">@Mapping</span></span><br><span class="line"><span class="keyword">public</span> <span class="meta">@interface</span> RequestMapping &#123;</span><br><span class="line">	<span class="function">String <span class="title">name</span><span class="params">()</span> <span class="keyword">default</span> ""</span>;</span><br><span class="line">	<span class="meta">@AliasFor</span>(<span class="string">"path"</span>)</span><br><span class="line">	String[] value() <span class="keyword">default</span> &#123;&#125;;</span><br><span class="line">	<span class="meta">@AliasFor</span>(<span class="string">"value"</span>)<span class="comment">//这个注解和path 标志制定的url地址，可以使用resulf请求的格式</span></span><br><span class="line">    <span class="comment">//spring-web/&#123;symbolicName:[a-z-]+&#125;-&#123;version:\d\.\d\.\d&#125;.&#123;extension:\.[a-z]&#125;</span></span><br><span class="line">	String[] path() <span class="keyword">default</span> &#123;&#125;;</span><br><span class="line">	RequestMethod[] method() <span class="keyword">default</span> &#123;&#125;;<span class="comment">//制定请求的方法，RequestMethod.DELETE</span></span><br><span class="line">	String[] params() <span class="keyword">default</span> &#123;&#125;;<span class="comment">//表示制定的请求参数中存在的参数组合，如myParam = myValue，表示给定的参数为指定的值才会返回</span></span><br><span class="line">    <span class="comment">//可以使用name ！= value 表示不等于的关系</span></span><br><span class="line">	String[] headers() <span class="keyword">default</span> &#123;&#125;;<span class="comment">// 制定头部包含的相关信息和上面的params类似，Referer=http://www.ifeng.com</span></span><br><span class="line">	String[] produces() <span class="keyword">default</span> &#123;&#125;;<span class="comment">//指定返回的内容类型，仅当request请求头中的(Accept)类型中包含该指定类型才返回，表示浏览器接受的相关资源</span></span><br><span class="line">    String[] consumes() <span class="keyword">default</span> &#123;&#125;;<span class="comment">//指定处理请求的提交内容类型（Content-Type），例如application/json, text/html</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>requestMapping:有简化版的各种注解@GetMapping，@PostMapping，@PutMapping，@DeleteMapping，@PatchMapping，指定了相关的method方法对应的各种请求</p>
<p>request 请求可以接受的请求可以通过通配符或者glob参数的方法进行匹配</p>
<p>spring mvc 的地址匹配方法满足一定的相关原则，可以使用通配符进行匹配</p>
<table>
<thead>
<tr>
<th>Wildcard</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>？</td>
<td>匹配任何单字符</td>
</tr>
<tr>
<td>*</td>
<td>匹配0或者任意数量的字符</td>
</tr>
<tr>
<td>**</td>
<td>匹配0或者更多的目录</td>
</tr>
</tbody>
</table>
<p>注意：spring将会按照匹配的字符最长的那个进行匹配，比如/*<em>/</em>.jsp 和/app/dir/*.jsp，在这个过程中，将会匹配后者。</p>
<p>注意：spring mvc 的匹配原则是按照后缀匹配的原则，比如一个url地址 /name这个地址，表示的就是/name.*，一定程度上实现了文件扩展名引用</p>
<p>如果使用了文件扩展名称这种东西如果想要配置请查看如下两个接口：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">useSuffixPatternMatching(<span class="keyword">false</span>), see PathMatchConfigurer</span><br><span class="line">favorPathExtension(<span class="keyword">false</span>), see ContentNeogiationConfigurer</span><br></pre></td></tr></table></figure>
<h5 id="在requestMethod中可以接收的数据"><a href="#在requestMethod中可以接收的数据" class="headerlink" title="在requestMethod中可以接收的数据"></a>在requestMethod中可以接收的数据</h5><p>接受的函数体中可以使用的注解和参数</p>
<ol>
<li>WebRequest, NativeWebRequest</li>
</ol>
<p>WebRequest是Spring Web MVC提供的统一请求访问接口，不仅仅可以访问请求相关数据（如参数区数据、请求头数据，但访问不到Cookie区数据），还可以访问会话和上下文中的数据；NativeWebRequest继承了WebRequest，并提供访问本地Servlet API的方法。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">webRequest</span><span class="params">(WebRequest webRequest, NativeWebRequest nativeWebRequest)</span> </span>&#123;</span><br><span class="line">    System.out.println(webRequest.getParameter(<span class="string">"test"</span>));<span class="comment">//①得到请求参数test的值  </span></span><br><span class="line">    webRequest.setAttribute(<span class="string">"name"</span>, <span class="string">"value"</span>, WebRequest.SCOPE_REQUEST);<span class="comment">//②  </span></span><br><span class="line">    System.out.println(webRequest.getAttribute(<span class="string">"name"</span>, WebRequest.SCOPE_REQUEST));</span><br><span class="line">    HttpServletRequest request =</span><br><span class="line">            nativeWebRequest.getNativeRequest(HttpServletRequest.class);<span class="comment">//③  </span></span><br><span class="line">    HttpServletResponse response =</span><br><span class="line">            nativeWebRequest.getNativeResponse(HttpServletResponse.class);</span><br><span class="line">    <span class="keyword">return</span> <span class="string">"success"</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ol start="2">
<li>javax.servlet.ServletRequest, javax.servlet.ServletResponse，javax.servlet.http.HttpSession</li>
</ol>
<p>javax 提供的具体接口注意其中的httpsession ，会话访问不是线程安全的。如果允许多个请求同时访问会话，请考虑将RequestMappingHandlerAdapter的“synchronizeOnSession”标志设置为“true”。</p>
<ol start="3">
<li>HttpMethod </li>
</ol>
<p>这个值中有传入的方式，比如get还是post</p>
<ol start="4">
<li>java.io.InputStream, java.io.Reader，java.io.OutputStream, java.io.Writer</li>
</ol>
<p>请求的原始请求数据流，和原始返回数据</p>
<ol start="5">
<li>@PathVariable和@MatrixVariable</li>
</ol>
<p>使用{}表示的url请求对应的相关参数,</p>
<p>@PathVariable 没什么好说的关键是@MatrixVariable ， 这个注解将会自动的匹配url 地址中 ;uuu=123;iii=333 这种参数，并且一定的程度下并不需要{jj}中指定的名称来匹配，当发生参数冲突的时候可以使用pathVar参数指定名称，目前有bug不记录了</p>
<ol start="5">
<li>@PathVariable,@MatrixVariable,@RequestParam，@RequestBody，@RequestHeader，@CookieValue，@RequestPart,@ModelAttribute,@SessionAttribute,@RequestAttribute</li>
</ol>
<ul>
<li>@PathVariable ,spring mvc提供了一套支持reastfulapi的方法，这套方法可以使用{}+通配符的方式，将url中的数据传递到method 对应的参数中</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@GetMapping</span>(<span class="string">"/owners/&#123;ownerId&#125;/pets/&#123;petId&#125;"</span>)</span><br><span class="line"><span class="function"><span class="keyword">public</span> Pet <span class="title">findPet</span><span class="params">(@PathVariable Long ownerId, @PathVariable Long petId)</span> </span>&#123;</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 可以使用这个方法实现全局的请求效果</span></span><br><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">"/owners/&#123;ownerId&#125;"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">OwnerController</span> </span>&#123;</span><br><span class="line">    <span class="meta">@GetMapping</span>(<span class="string">"/pets/&#123;petId&#125;"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> Pet <span class="title">findPet</span><span class="params">(@PathVariable Long ownerId, @PathVariable(petId)</span> Long petId) </span>&#123;</span><br><span class="line">        <span class="comment">// ...</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>注意：{}里面的值值也可以使用正则表达式进行相关的配置，格式：{varName:regex}</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@GetMapping</span>(<span class="string">"/&#123;name:[a-z-]+&#125;-&#123;version:\\d\\.\\d\\.\\d&#125;&#123;ext:\\.[a-z]+&#125;"</span>)</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">handle</span><span class="params">(@PathVariable String version, @PathVariable String ext)</span> </span>&#123;&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li><p>@MatrixVariable，这个注解将会自动的匹配url地址中;uuu=123;iii=333这种参数，并且一定的程度下并不需要{jj}中指定的名称来匹配，当发生参数冲突的时候可以使用pathVar参数指定名称，目前有bug不记录了</p>
</li>
<li><p>@RequestParam接口将会自动的将传入到指定的地址中 如果使用这个注解标识Map&lt;String, String&gt; or MultiValueMap&lt;String, String&gt;，将会将所有的属性注入进来<br>A） 常用来处理简单类型的绑定，通过Request.getParameter() 获取的String可直接转换为简单类型的情况（ String–&gt; 简单类型的转换操作由ConversionService配置的转换器来完成）；因为使用request.getParameter()方式获取参数，所以可以处理get 方式中queryString的值，也可以处理post方式中 body data的值；<br>B）用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容，提交方式GET、POST；<br>C) 该注解有两个属性： value、required； value用来指定要传入值的id名称，required用来指示参数是否必须绑定；</p>
</li>
<li><p>@RequestBody 该注解常用来处理Content-Type: 不是application/x-www-form-urlencoded编码的内容，例如application/json, application/xml等；它是通过使用HandlerAdapter 配置的HttpMessageConverters来解析post data body，然后绑定到相应的bean上的。</p>
</li>
</ul>
<p>使用：HttpMessageConverter接口，需要开启&lt;mvc:annotation-driven  /&gt;。 AnnotationMethodHandlerAdapter将会初始化7个转换器，可以通过调用AnnotationMethodHandlerAdapter的getMessageConverts()方法来获取转换器的一个集合 List<httpmessageconverter>，这7个转化器如下：</httpmessageconverter></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">ByteArrayHttpMessageConverter </span><br><span class="line">StringHttpMessageConverter </span><br><span class="line">ResourceHttpMessageConverter </span><br><span class="line">SourceHttpMessageConverter </span><br><span class="line">XmlAwareFormHttpMessageConverter </span><br><span class="line">Jaxb2RootElementHttpMessageConverter </span><br><span class="line">MappingJacksonHttpMessageConverter</span><br></pre></td></tr></table></figure>
<p>PS:Spring默认的json协议解析由Jackson完成。 这种方式的时候默认使用的就是json解析。</p>
<p>问题 如何扩展</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Controller</span>  </span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PersonController</span> </span>&#123;    </span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"/person/login"</span>, method = RequestMethod.POST)  </span><br><span class="line">    <span class="keyword">public</span> <span class="meta">@ResponseBody</span>  </span><br><span class="line">    <span class="function">Person <span class="title">login</span><span class="params">(@RequestBody Person person)</span> </span>&#123;  </span><br><span class="line">        <span class="keyword">return</span> person;  </span><br><span class="line">    &#125;  </span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//注意序列化接口</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Person</span> <span class="keyword">implements</span> <span class="title">Serializable</span> </span>&#123;  </span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> id;  </span><br><span class="line">    <span class="keyword">private</span> String name;  </span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">boolean</span> status;  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Person</span><span class="params">()</span> </span>&#123;  </span><br><span class="line">        <span class="comment">// do nothing  </span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>ps：如果使用xml 方式，需要使用注解</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@XmlRootElement</span>(name = <span class="string">"Person"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Person</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> age;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>&#123; <span class="keyword">return</span> name;    &#125;</span><br><span class="line">    <span class="meta">@XmlElement</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>&#123; <span class="keyword">this</span>.name = name;    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getAge</span><span class="params">()</span> </span>&#123; <span class="keyword">return</span> age;    &#125;</span><br><span class="line">    <span class="meta">@XmlElement</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setAge</span><span class="params">(<span class="keyword">int</span> age)</span> </span>&#123; <span class="keyword">this</span>.age = age;    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>注意 这里如果要使用注意spring mvc 环境的配置</p>
<p>使用xml 进行配置</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">context:annotation-config</span>&gt;</span><span class="tag">&lt;/<span class="name">context:annotation-config</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">context:component-scan</span> <span class="attr">base-package</span>=<span class="string">"com"</span>&gt;</span><span class="tag">&lt;/<span class="name">context:component-scan</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">mvc:annotation-driven</span>&gt;</span><span class="tag">&lt;/<span class="name">mvc:annotation-driven</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">mvc:default-servlet-handler</span>/&gt;</span></span><br></pre></td></tr></table></figure>
<p>使用java 进行配置</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//注意要将sevlet放入 WebApplicationContext中</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyWebApplication</span> <span class="keyword">implements</span> <span class="title">WebApplicationInitializer</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onStartup</span><span class="params">(ServletContext servletContext)</span> <span class="keyword">throws</span> ServletException </span>&#123;</span><br><span class="line">        AnnotationConfigWebApplicationContext annotationConfigWebApplicationContext=</span><br><span class="line">                <span class="keyword">new</span> AnnotationConfigWebApplicationContext();</span><br><span class="line">        annotationConfigWebApplicationContext.register(AppConfig.class);</span><br><span class="line">        annotationConfigWebApplicationContext.setServletContext(servletContext);</span><br><span class="line">        annotationConfigWebApplicationContext.refresh();</span><br><span class="line">        ServletRegistration.Dynamic dynamic=servletContext.addServlet(<span class="string">"dispatcher"</span>,<span class="keyword">new</span> DispatcherServlet(annotationConfigWebApplicationContext));</span><br><span class="line">        dynamic.setLoadOnStartup(<span class="number">1</span>);</span><br><span class="line">        dynamic.addMapping(<span class="string">"/"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//自动化配置</span></span><br><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="meta">@ComponentScan</span></span><br><span class="line"><span class="meta">@EnableWebMvc</span><span class="comment">//使用EnableWebMVC注解自动的注入的相关的属性</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AppConfig</span>  </span>&#123; &#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>@RequestPart  和文件上传相关，难度有点大，和http协议相关的暂时不考虑</li>
</ul>
<ul>
<li><p>@RequestHeader 将http协议中相关的头注入到指定的数据中</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@GetMapping</span>(<span class="string">"/demo"</span>)</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">handle</span><span class="params">(</span></span></span><br><span class="line"><span class="function"><span class="params">        @RequestHeader(<span class="string">"Accept-Encoding"</span>)</span> String encoding,</span></span><br><span class="line"><span class="function">        @<span class="title">RequestHeader</span><span class="params">(<span class="string">"Keep-Alive"</span>)</span> <span class="keyword">long</span> keepAlive) </span>&#123;</span><br><span class="line">    <span class="comment">//...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>@ModelAttribute</p>
</li>
</ul>
<p>在使用model view 场景下，有如下的一应用</p>
<ul>
<li><ol>
<li>全局model配置，在获得请求/helloWorld 后，populateModel方法在helloWorld方法之前先被调用，它把请求参数（/helloWorld?abc=text）加入到一个名为attributeName的model属性中，在它执行后 helloWorld被调用，返回视图名helloWorld和model已由@ModelAttribute方法生产好了。<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Controller</span> </span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">HelloWorldController</span> </span>&#123; </span><br><span class="line">    <span class="meta">@ModelAttribute</span> </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">populateModel</span><span class="params">(@RequestParam String abc, Model model)</span> </span>&#123; </span><br><span class="line">         model.addAttribute(<span class="string">"attributeName"</span>, abc); </span><br><span class="line">    &#125; </span><br><span class="line">    <span class="meta">@ModelAttribute</span>(<span class="string">"attributeName"</span>) </span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">addAccount</span><span class="params">(@RequestParam String abc)</span> </span>&#123; </span><br><span class="line">        <span class="keyword">return</span> abc; </span><br><span class="line">    &#125; </span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"/helloWorld"</span>) </span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">helloWorld</span><span class="params">()</span> </span>&#123; </span><br><span class="line">       <span class="keyword">return</span> <span class="string">"helloWorld"</span>; </span><br><span class="line">    &#125;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
</li>
</ol>
</li>
<li><ol start="2">
<li>指派model配置  返回 helloworld.do视图，有一个model 参数是attributeName和值 hi</li>
</ol>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Controller</span> </span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">HelloWorldController</span> </span>&#123; </span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"/helloWorld.do"</span>) </span><br><span class="line">    <span class="meta">@ModelAttribute</span>(<span class="string">"attributeName"</span>) </span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">helloWorld</span><span class="params">()</span> </span>&#123; </span><br><span class="line">        <span class="keyword">return</span> <span class="string">"hi"</span>; </span><br><span class="line">    &#125; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li><ol start="3">
<li>绑定application/x-www-form-urlencoded 提交的请求中 的值到对象中 支持user.xxx,user2.ddd 嵌套对应法</li>
</ol>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Controller</span> </span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">HelloWorldController</span> </span>&#123; </span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"/helloWorld"</span>) </span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">helloWorld</span><span class="params">(@ModelAttribute User user)</span> </span>&#123; </span><br><span class="line">        <span class="keyword">return</span> <span class="string">"helloWorld"</span>; </span><br><span class="line">     &#125; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>@CookieValue  和之前的相同，就是将cookie中相关的数据拿出来</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@GetMapping</span>(<span class="string">"/demo"</span>)</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">handle</span><span class="params">(@CookieValue(<span class="string">"JSESSIONID"</span>)</span> String cookie) </span>&#123;&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>HttpEntity<b> HttpEntity或多或少与使用@RequestBody相同，但基于公开请求标头和主体的容器对象。</b></li>
</ul>
<p>注意这个方法多是用于post请求用来针对ajax序列化的json对象解析，其中有一个getbody方法</p>
 <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@PostMapping</span>(<span class="string">"/accounts"</span>)</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">handle</span><span class="params">(HttpEntity&lt;Account&gt; entity)</span> </span>&#123;</span><br><span class="line">    Account account=entity.getBody();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>@InitBinder  spring自带的数据处理模块</li>
</ul>
<p>由@InitBinder表示的方法，可以对WebDataBinder对象进行初始化。WebDataBinder是DataBinder的子类，用于完成由表单到JavaBean属性的绑定。<br>@InitBinder方法不能有返回值，它必须盛名为void。<br>@InitBinder方法的参数通常是WebDataBinder，@InitBinder可以对WebDataBinder进行初始化。</p>
<p>注意这个注解只是针对这个controller中的方法起作用，无法针对所有的controller</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BinderAction</span> </span>&#123;</span><br><span class="line">    <span class="meta">@RequestMapping</span>(<span class="string">"/sb2.do"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">doTest</span><span class="params">(@RequestParam(value=<span class="string">"name"</span>)</span>String name,@<span class="title">RequestParam</span><span class="params">(value=<span class="string">"age"</span>)</span><span class="keyword">double</span> age,@<span class="title">RequestParam</span><span class="params">(value=<span class="string">"nowTime"</span>)</span>Date nowTime)</span>&#123;</span><br><span class="line">        System.err.println(<span class="string">"name:"</span> + name);</span><br><span class="line">        System.err.println(<span class="string">"age:"</span> + age);</span><br><span class="line">        System.err.println(<span class="string">"nowTime:"</span> + nowTime);</span><br><span class="line">    &#125;    </span><br><span class="line">    <span class="meta">@InitBinder</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">initBinder</span><span class="params">(WebDataBinder binder)</span></span>&#123;</span><br><span class="line">        binder.registerCustomEditor(Date.class, <span class="keyword">new</span> CustomDateEditor(<span class="keyword">new</span> SimpleDateFormat(<span class="string">"yyyy-MM-dd"</span>), <span class="keyword">true</span>));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">@InitBinder</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">initBinder</span><span class="params">(WebDataBinder binder)</span> </span>&#123;</span><br><span class="line">        binder.addCustomFormatter(<span class="keyword">new</span> DateFormatter(<span class="string">"yyyy-MM-dd"</span>));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>注意WebDataBinder这个对象，这个对象拥有一个方法registerCustomEditor,这个方法将会自动的配置属性映射器，将相关的属性映射到指定的位置，属性映射器可以使用如下的方法进行自定义<br>实现PropertyEditor或者重写PropertyEditorSupport对象中的方法，注意这种方法只能实现string到对象的转换 setValue中就是转化后的对象，setAsText传入的就是传入的url字符串</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//实现PropertyEditor或者重写PropertyEditorSupport对象中的方法</span></span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.propertyeditors.PropertiesEditor;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DoubleEditor</span> <span class="keyword">extends</span> <span class="title">PropertyEditorSupport</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setAsText</span><span class="params">(String text)</span> <span class="keyword">throws</span> IllegalArgumentException </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (text == <span class="keyword">null</span> || text.equals(<span class="string">""</span>)) &#123;</span><br><span class="line">            text = <span class="string">"0"</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        setValue(Double.parseDouble(text));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getAsText</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> getValue().toString();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>同时WebDataBinder这个对象，这个对象拥有一个addCustomFormatter 可以直接使用formatter进行参数转化本质上是相同的</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@InitBinder</span></span><br><span class="line"><span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">initBinder</span><span class="params">(WebDataBinder binder)</span> </span>&#123;</span><br><span class="line">    binder.addCustomFormatter(<span class="keyword">new</span> DateFormatter(<span class="string">"yyyy-MM-dd"</span>));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>addCustomFormatter本质上还是和registerCustomEditor是一样的见源代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addCustomFormatter</span><span class="params">(Formatter&lt;?&gt; formatter)</span> </span>&#123;</span><br><span class="line">	FormatterPropertyEditorAdapter adapter = <span class="keyword">new</span> FormatterPropertyEditorAdapter(formatter);</span><br><span class="line">	getPropertyEditorRegistry().registerCustomEditor(adapter.getFieldType(), adapter);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="RequestMapping-方法返回值中的参数"><a href="#RequestMapping-方法返回值中的参数" class="headerlink" title="@RequestMapping 方法返回值中的参数"></a>@RequestMapping 方法返回值中的参数</h4><ol>
<li>@ResponseBody</li>
</ol>
<ol start="2">
<li>HttpEntity<b>, ResponseEntity<b></b></b></li>
</ol>
<ul>
<li><p>HttpEntity<b> HttpEntity或多或少与使用@RequestBody相同，但基于公开请求标头和主体的容器对象。</b></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@PostMapping</span>(<span class="string">"/accounts"</span>)</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">handle</span><span class="params">(HttpEntity&lt;Account&gt; entity)</span> </span>&#123;</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>ResponseEntity<b> 这个类其实是对 http请求的一个封装，封装了http的报头，状态码，http code 等数据，本质上是一种通信协议</b></p>
</li>
</ul>
<p>如果使用的不是string类型，那么将会是一种类似rpc协议</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//客户端</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">client</span><span class="params">()</span></span>&#123;</span><br><span class="line">    RestTemplate restTemplate = <span class="keyword">new</span> RestTemplate();</span><br><span class="line">    ResponseEntity&lt;String&gt; entity = restTemplate.getForEntity(<span class="string">"http://example.com"</span>, String.class);</span><br><span class="line">    String body = entity.getBody();</span><br><span class="line">    MediaType contentType = entity.getHeaders().getContentType();</span><br><span class="line">    HttpStatus statusCode = entity.getStatusCode();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//服务端</span></span><br><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">"/handle"</span>)</span><br><span class="line"><span class="function"><span class="keyword">public</span> ResponseEntity&lt;String&gt; <span class="title">handle</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    URI location = ...;</span><br><span class="line">    HttpHeaders responseHeaders = <span class="keyword">new</span> HttpHeaders();</span><br><span class="line">    responseHeaders.setLocation(location);</span><br><span class="line">    responseHeaders.set(<span class="string">"MyResponseHeader"</span>, <span class="string">"MyValue"</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> ResponseEntity&lt;String&gt;(<span class="string">"Hello World"</span>, responseHeaders,   HttpStatus.CREATED);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>当使用string类型的时候就和@ResponceBody+@ResponseStatus 相同</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">"/handle"</span>)</span><br><span class="line"><span class="function"><span class="keyword">public</span> ResponseEntity&lt;String&gt; <span class="title">handle</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    URI location = ...;</span><br><span class="line">    <span class="keyword">return</span> ResponseEntity.created(location).header(<span class="string">"MyResponseHeader"</span>,  <span class="string">"MyValue"</span>).body(<span class="string">"Hello World"</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ol start="3">
<li>HttpHeaders</li>
</ol>
<p>返回一个封装的httpheaders的头，这个类有一个set方法，制定方法的头和内容，如果想深入的使用，需要精通http协议</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> HttpHeaders <span class="title">getHeader</span><span class="params">()</span></span>&#123;</span><br><span class="line">    HttpHeaders httpHeaders = <span class="keyword">new</span> HttpHeaders();</span><br><span class="line">    <span class="keyword">return</span> httpHeaders;   </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ol start="4">
<li>string</li>
</ol>
<p>最简单的一个方法，spring将会使用这个字符串找到对应的view</p>
<ol start="5">
<li>java.util.Map, org.springframework.ui.Model，@ModelAttribute</li>
</ol>
<p>spring mvc modelandview体系的东西</p>
<ol start="6">
<li><p>ResponseBodyEmitter, SseEmitter， StreamingResponseBody</p>
</li>
<li><p>Reactive types — Reactor, RxJava, or others via ReactiveAdapterRegistry</p>
</li>
<li><p>@ResponseStatus(HttpStatus.CREATED)</p>
</li>
</ol>
<p>制定返回值的头部信息, 比如制定401 402 这种http code</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/10/09/spring5-springMVC使用简介/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="kyssion">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="kyssion-blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/10/09/spring5-springMVC使用简介/" itemprop="url">未命名</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-10-09T15:22:37+08:00">
                2018-10-09
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="spring5-springMVC使用"><a href="#spring5-springMVC使用" class="headerlink" title="spring5-springMVC使用"></a>spring5-springMVC使用</h2><p>使用一个基本demo进行相关的整理</p>
<h3 id="java-controller"><a href="#java-controller" class="headerlink" title="java  controller"></a>java  controller</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//当不是用配置文件的时候我们将不需要使用接口直接使用一个注释</span></span><br><span class="line"><span class="meta">@Controller</span><span class="comment">//这个注释讲这个bean配制成一个控制器-注解没有标记只是起到了一个标记的作用</span></span><br><span class="line"><span class="comment">//注意要让这个注解起到作用需要早xml文件中进行配置</span></span><br><span class="line"><span class="meta">@EnableWebMvc</span></span><br><span class="line"><span class="comment">//@RequestMapping(value="/sdf")//这样下面的所有方法的起始地址就是/sdf</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Noconfig</span> </span>&#123;</span><br><span class="line">    <span class="meta">@RequestMapping</span>(value=<span class="string">"/mappingOne"</span>,method=&#123;RequestMethod.GET,RequestMethod.PUT&#125;)</span><br><span class="line">    <span class="comment">//注解中属性-value：表示需要匹配的url的格式。 //method：表示所需处理请求的http 协议(如get,post,put,delete等)，可选值为RequestMethod这个enum的值。</span></span><br><span class="line">    <span class="comment">//params:格式为”paramname=paramvalue” 或 “paramname!=paramvalue”。不带参数则表示paramvalue可以为任意值。</span></span><br><span class="line">    <span class="comment">//  如params =  &#123;"param1=1","param2!=2","param3"&#125;,</span></span><br><span class="line">    <span class="comment">//  表示对应的url必须包括param1,param2,param3三个参数，其中param1的值必须为1，param2的值不能为2，param3的值可以为任意值。</span></span><br><span class="line">    <span class="comment">//headers:用来限定对应的reqeust请求的headers中必须包括的内容，例如</span></span><br><span class="line">    <span class="comment">//  headers=&#123;"Connection=keep-alive"&#125;, 表示请求头中的connection的值必须为keep-alive。</span></span><br><span class="line">    <span class="comment">//  定义的方法中可以讲需要的属性直接写进去,从而直接能得到,spring底层使用反射技术能动态的知道你需要什么属性</span></span><br><span class="line">    <span class="comment">//spring支持的属性</span></span><br><span class="line">    <span class="comment">//     java- HttpServletRequest HttpServletServletResponse HttpSession Locale </span></span><br><span class="line">    <span class="comment">//           inputstream reader outputStream writer Map </span></span><br><span class="line">    <span class="comment">//spring的属性WebRequest NativeWebRequest ModeMap model- </span></span><br><span class="line">    <span class="comment">//         表单对象(气势model 或者 map 或者 ModeMap就是封装了request.setAttrbute方法)</span></span><br><span class="line">    <span class="comment">//注意这些参数都是在一个请求中有效也就是说只要获取了对台进行操作在一次请求转发过程中都能获得和进行操作</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> ModelAndView <span class="title">mappingOne</span><span class="params">(HttpServletRequest request,HttpServletResponse response ,ModelMap modelMap)</span></span>&#123;</span><br><span class="line">        System.out.println(request.getParameter(<span class="string">"one"</span>));</span><br><span class="line">        modelMap.addAttribute(<span class="string">"xixi"</span>, <span class="string">"123145"</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> ModelAndView(<span class="string">"myModeAndView"</span>);</span><br><span class="line">        <span class="comment">//处理方法可以返回的类型</span></span><br><span class="line">        <span class="comment">//Modelandview Medel map View 代表逻辑属兔名称的String void </span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">@RequestMapping</span>(value=<span class="string">"mappingTwo/&#123;id&#125;/&#123;xixi&#125;"</span>,method=&#123;RequestMethod.GET,RequestMethod.POST&#125;)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">mappingTwo</span><span class="params">(@PathVariable()</span> <span class="keyword">int</span> id,@PathVariable String xixi)</span>&#123;</span><br><span class="line">        <span class="comment">//一种解析url的方法 -讲url地址地址中的方法进行解析处理  名称要相互匹配要相同</span></span><br><span class="line">        <span class="comment">//name(value) - 指定绑定的参数</span></span><br><span class="line"></span><br><span class="line">        System.out.println(id+<span class="string">"  "</span>+xixi);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">@RequestMapping</span>(value=<span class="string">"mappingThree"</span>,method=&#123;RequestMethod.GET,RequestMethod.POST&#125;)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">mappingThree</span><span class="params">(@RequestParam(value=<span class="string">"sadf"</span>)</span> <span class="keyword">int</span> id,@RequestParam String hehe)</span>&#123;</span><br><span class="line">        <span class="comment">//讲url 地址中的响应字子自动和函数中的参数相匹配 注意名称要相互匹配要相同</span></span><br><span class="line">        <span class="comment">//name-指定参数中那个元素和者个进行绑定 value 是他的别名</span></span><br><span class="line">        <span class="comment">//defaulfvalue -  指定默认的参数</span></span><br><span class="line">        System.out.println(id+<span class="string">"  "</span>+hehe);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//spring 重定向</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">mappingFour</span><span class="params">(RedirectAttributes redirectAttributes)</span></span>&#123;</span><br><span class="line">        redirectAttributes.addFlashAttribute(<span class="string">"xx"</span>, <span class="string">"asdfasdf"</span>);<span class="comment">//使用这个方法可以实现内部的重定向属性传递方法</span></span><br><span class="line">        <span class="comment">//在spring中返回字符串使用rediret开头的时候表使用了重定向技术</span></span><br><span class="line">        <span class="keyword">return</span> <span class="string">"redirect:/xxxx/"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//设置请求头为utf8 解决中文乱码</span></span><br><span class="line">    <span class="meta">@RequestMapping</span>(value=<span class="string">"name"</span>,produces = <span class="string">"application/json;charset=utf-8"</span>)</span><br><span class="line">    <span class="meta">@ResponseBody</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">hehe</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"sdfsdf"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//model and view </span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">springModelandView</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="comment">//modelandview 其实是将model和view 封装在一个如果想使用需要使用get方法生成</span></span><br><span class="line">        ModelAndView modelAndView = <span class="keyword">new</span> ModelAndView();</span><br><span class="line">        <span class="comment">//modelAndView= new ModelAndView(viewName, model)</span></span><br><span class="line">        modelAndView.addAllObjects(<span class="keyword">new</span> HashMap&lt;String,String&gt;());</span><br><span class="line">        modelAndView.addObject(<span class="string">"xxx"</span>, <span class="string">"xxx"</span>);</span><br><span class="line">        View view=modelAndView.getView();</span><br><span class="line">        ModelMap map =modelAndView.getModelMap();</span><br><span class="line">        modelAndView.setView(view);</span><br><span class="line">        modelAndView.setViewName(<span class="string">"xxx"</span>);</span><br><span class="line">        <span class="comment">//modelMap--其实是map的实现类而已</span></span><br><span class="line">        ModelMap map2= <span class="keyword">new</span> ModelMap();</span><br><span class="line">        map.addAllAttributes(<span class="keyword">new</span> HashMap&lt;String,String&gt;());</span><br><span class="line">        map.addAttribute(<span class="string">"xx"</span>, <span class="string">"heh"</span>);</span><br><span class="line">        <span class="comment">//view---没什么用就当是字符串就好了</span></span><br><span class="line">        View view2 = <span class="keyword">new</span> View() &#123;</span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">render</span><span class="params">(Map&lt;String, ?&gt; arg0, HttpServletRequest arg1, HttpServletResponse arg2)</span> <span class="keyword">throws</span> Exception </span>&#123;&#125;</span><br><span class="line">            <span class="function"><span class="keyword">public</span> String <span class="title">getContentType</span><span class="params">()</span> </span>&#123;<span class="keyword">return</span> <span class="keyword">null</span>;&#125;</span><br><span class="line">        &#125;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>其他一些注解</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@EnableWebMvc</span><span class="comment">//开起一些viewResolver或者MessageConverter等</span></span><br><span class="line"><span class="meta">@RestController</span><span class="comment">// 相当于Controller和ResponseBody的结合，使用在类上面，导致所有的返回值都是body类型</span></span><br></pre></td></tr></table></figure>
<h3 id="springMVC-webapplication-配置文件"><a href="#springMVC-webapplication-配置文件" class="headerlink" title="springMVC webapplication 配置文件"></a>springMVC webapplication 配置文件</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span><br><span class="line"><span class="tag">&lt;<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">"http://www.springframework.org/schema/beans"</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:mvc</span>=<span class="string">"http://www.springframework.org/schema/mvc"</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:context</span>=<span class="string">"http://www.springframework.org/schema/context"</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xsi:schemaLocation</span>=<span class="string">"http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd</span></span></span><br><span class="line"><span class="tag"><span class="string">		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd</span></span></span><br><span class="line"><span class="tag"><span class="string">		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">context:annotation-config</span>&gt;</span><span class="tag">&lt;/<span class="name">context:annotation-config</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 激活spring关于annotation的DI --&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--  	注册 DefaultAnnotationHandlerMapping 和AnnotationMethodHandlerAdapter两个bean，</span></span><br><span class="line"><span class="comment">    及一系列缺省的messageconverter(需要classpath中有相应的lib包的支持)</span></span><br><span class="line"><span class="comment">            并且启用了controler中的url请求参数绑定参数绑定</span></span><br><span class="line"><span class="comment">     --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">mvc:annotation-driven</span>&gt;</span><span class="tag">&lt;/<span class="name">mvc:annotation-driven</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 注册HandlerInterceptors 或 WebRequestInterceptors 拦截器来拦截request请求 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">mvc:interceptors</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">mvc:interceptor</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">mvc:mapping</span> <span class="attr">path</span>=<span class="string">"/*"</span> /&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">bean</span> <span class="attr">class</span>=<span class="string">"p_spring事务.hasconfig.CustomerTransforHasConfig"</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">mvc:interceptor</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">mvc:interceptors</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--将spring mvc的DispatcherServlet替换当前web server的缺省的Servlet。</span></span><br><span class="line"><span class="comment">        这样做的好处是定义spring mvc处理请求时，不再需要定义*.do,*.action,/action/*之类的url-pattern，</span></span><br><span class="line"><span class="comment">        而是可以直接定义为&lt;url-pattern&gt;/&lt;/url-pattern&gt; </span></span><br><span class="line"><span class="comment">        （注意不是&lt;url-pattern&gt;/*&lt;/url-pattern&gt;，/*将匹配所有请求而导致所有jsp和静态内容都无法正常显示，而/只匹配缺省的servlet）。 </span></span><br><span class="line"><span class="comment">        常见web 容器的default servlet name spring mvc中都能自动识别，</span></span><br><span class="line"><span class="comment">        但对于不常见的web 容器或default servlet name 被改写过的，则需增加参数 default-servlet-name来指定对应的servlet name. </span></span><br><span class="line"><span class="comment">        需与&lt;mvc:annotation-driven/&gt;一起使用 </span></span><br><span class="line"><span class="comment">    --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">mvc:default-servlet-handler</span>/&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 将指定路径的请求直接转到对应的view上，而不需要特定的controller来处理请求 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">mvc:view-controller</span> <span class="attr">path</span>=<span class="string">"/hello/*"</span> <span class="attr">view-name</span>=<span class="string">"helloWorld"</span>/&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 将指定URL 的匹配模式来访问静态文件 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">mvc:resources</span> <span class="attr">mapping</span>=<span class="string">"/js/**"</span> <span class="attr">location</span>=<span class="string">"/WEB-INF/js/"</span> <span class="attr">cache-period</span>=<span class="string">"3600"</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="web-xml配置文件"><a href="#web-xml配置文件" class="headerlink" title="web.xml配置文件"></a>web.xml配置文件</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span><br><span class="line"><span class="tag">&lt;<span class="name">web-app</span> <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></span></span><br><span class="line"><span class="tag">         <span class="attr">xmlns</span>=<span class="string">"http://xmlns.jcp.org/xml/ns/javaee"</span></span></span><br><span class="line"><span class="tag">         <span class="attr">xsi:schemaLocation</span>=<span class="string">"http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"</span></span></span><br><span class="line"><span class="tag">         <span class="attr">id</span>=<span class="string">"WebApp_ID"</span> <span class="attr">version</span>=<span class="string">"3.1"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">display-name</span>&gt;</span>Spring和SpringMVC和SpringBoot和Mybatis<span class="tag">&lt;/<span class="name">display-name</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">welcome-file-list</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">welcome-file</span>&gt;</span>index.html<span class="tag">&lt;/<span class="name">welcome-file</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">welcome-file</span>&gt;</span>index.htm<span class="tag">&lt;/<span class="name">welcome-file</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">welcome-file</span>&gt;</span>index.jsp<span class="tag">&lt;/<span class="name">welcome-file</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">welcome-file</span>&gt;</span>default.html<span class="tag">&lt;/<span class="name">welcome-file</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">welcome-file</span>&gt;</span>default.htm<span class="tag">&lt;/<span class="name">welcome-file</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">welcome-file</span>&gt;</span>default.jsp<span class="tag">&lt;/<span class="name">welcome-file</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">welcome-file-list</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 在spring体系中 root appliction使用 ContextLoaderListener进行加载 webapplication使用 Dispatchar加载  webappliction会继承 rootapplication 的继承 --&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 和ContextLoaderListener 相互对应实现对除了mvc层的控制 自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">context-param</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">param-name</span>&gt;</span>contextConfigLocation<span class="tag">&lt;/<span class="name">param-name</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 如果不写默认的配置路径是 /WEB-INF/applicationContext.xml 下面的配置使用通配符表示使用所有的配置文件--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">param-value</span>&gt;</span>/WEB-INF/application.xml<span class="tag">&lt;/<span class="name">param-value</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">context-param</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">listener</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">listener-class</span>&gt;</span>org.springframework.web.context.ContextLoaderListener<span class="tag">&lt;/<span class="name">listener-class</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">listener</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 添加文字控制servlet层 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">filter</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">filter-name</span>&gt;</span>CharacterEncodingFilter<span class="tag">&lt;/<span class="name">filter-name</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">filter-class</span>&gt;</span>org.springframework.web.filter.CharacterEncodingFilter<span class="tag">&lt;/<span class="name">filter-class</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">init-param</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">param-name</span>&gt;</span>encoding<span class="tag">&lt;/<span class="name">param-name</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">param-value</span>&gt;</span>utf-8<span class="tag">&lt;/<span class="name">param-value</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">init-param</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">filter</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">filter-mapping</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">filter-name</span>&gt;</span>CharacterEncodingFilter<span class="tag">&lt;/<span class="name">filter-name</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">url-pattern</span>&gt;</span>/*<span class="tag">&lt;/<span class="name">url-pattern</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">filter-mapping</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">servlet</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>springMVC<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-class</span>&gt;</span>org.springframework.web.servlet.DispatcherServlet<span class="tag">&lt;/<span class="name">servlet-class</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 设置webappliction 的默认加载位置 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">init-param</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">param-name</span>&gt;</span>contextConfigLocation<span class="tag">&lt;/<span class="name">param-name</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">param-value</span>&gt;</span>/WEB-INF/springMVC-servlet.xml<span class="tag">&lt;/<span class="name">param-value</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">init-param</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">load-on-startup</span>&gt;</span>1<span class="tag">&lt;/<span class="name">load-on-startup</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">servlet</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">servlet-mapping</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>springMVC<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">url-pattern</span>&gt;</span>/<span class="tag">&lt;/<span class="name">url-pattern</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">servlet-mapping</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">web-app</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="springMVC过滤器"><a href="#springMVC过滤器" class="headerlink" title="springMVC过滤器"></a>springMVC过滤器</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> q_springMVC.过滤器;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.HttpServletRequest;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.HttpServletResponse;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.web.servlet.HandlerInterceptor;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.servlet.ModelAndView;</span><br><span class="line"><span class="comment">//实现自定义spring拦截器必须继承的接口HandlerINterceptor  但是如果要使用这个拦截器需要将这个拦截器注册到相应的springmvc的配置文件(不是web.xml)中去</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyspringFilter</span> <span class="keyword">implements</span> <span class="title">HandlerInterceptor</span></span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">afterCompletion</span><span class="params">(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)</span></span></span><br><span class="line"><span class="function">            <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        <span class="comment">// TODO Auto-generated method stub</span></span><br><span class="line">        <span class="comment">//请求结束的时候调用的方法 object类的效果和上面的的相同 Exception表示抛出的异常</span></span><br><span class="line">        <span class="comment">//试图完成后执行的各种参数</span></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span><span class="comment">//在业务处理器处理请求执行完成后,生成视图之前执行</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">postHandle</span><span class="params">(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)</span></span></span><br><span class="line"><span class="function">            <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        <span class="comment">// TODO Auto-generated method stub</span></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span><span class="comment">//这个方法表示是否见当前的请求拦截下来 true表示不false 拦截  参数中有一个object 的类i表示将要进行拦截对象的实例</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">preHandle</span><span class="params">(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        <span class="comment">// TODO Auto-generated method stub</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="相关流程简介"><a href="#相关流程简介" class="headerlink" title="相关流程简介"></a>相关流程简介</h3><p><img src="blogimg/springMVC/1.png" alt=""></p>
<p><img src="blogimg/springMVC/2.png" alt=""></p>
<p><img src="blogimg/springMVC/3.png" alt=""></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/10/09/java-classpath的理解/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="kyssion">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="kyssion-blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/10/09/java-classpath的理解/" itemprop="url">未命名</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-10-09T15:22:37+08:00">
                2018-10-09
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>这次写这一片篇文章的起因是对老大搭建的底层框架存在怀疑结果然后自己照着老大的方法运行竟然可以成功原因的费解。因此在这里记录一下自己的疑问解惑过程。</p>
<h3 id="classpath到底是啥"><a href="#classpath到底是啥" class="headerlink" title="classpath到底是啥"></a>classpath到底是啥</h3><p>我的理解classpath 其实是 java 的一种虚拟的文件目录结构，java通过包名（相当于路径名称），针对每一个classpath地址（java支持多个classpath地址）进行资源管理的一种抽象路径</p>
<h4 id="1-针对于资源加载"><a href="#1-针对于资源加载" class="headerlink" title="1. 针对于资源加载"></a>1. 针对于资源加载</h4><p>最直观是我在mybatis中配置map xml文件路径的时候会加上</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">classpath:map/*.xml</span><br></pre></td></tr></table></figure>
<p>这么做将会让mybati所有所有的classpat路径下的对应的map/目录中的文件</p>
<p>也就是说如果jar包1 中有map/目录，并且jar2 中也有这个目录 jar1 引用了jar2，当jar1运行的时候并且jar2又在classpath当中的时候将会自动的搜索两个jar包classpath相对路径下的，map/目录下的信息</p>
<blockquote>
<p>注意如果两个文件名称是相同的（包括classpath对应的相对路径），将会以先加载的文件优先</p>
</blockquote>
<h4 id="2-对与和class对象加载"><a href="#2-对与和class对象加载" class="headerlink" title="2. 对与和class对象加载"></a>2. 对与和class对象加载</h4><p>jvm的类加载分三中方式：</p>
<ol>
<li>系统级别：rt.jar</li>
<li>扩展级别：java_home/jre/lib/ext/目录下的jar文件</li>
<li>应用级别：环境变量中的classpath或javac   java中的参数指定java   -classpath        …</li>
<li><p>或者自己写ClassLoader加载。</p>
<p>前面2中是JVM自动处理。其中第二种是为了处理Java的classpath灾难而提供的解决方案。</p>
</li>
</ol>
<p>实际上java虚拟机是由java luncher初始化的，也就是java（或java.exe）这个程序来做的。</p>
<p>虚拟机按以下顺序搜索并装载所有需要的类：</p>
<ol>
<li>引导类:组成java平台的类，包含rt.jar和i18n.jar中的类。</li>
<li>扩展类：使用java扩展机制的类，都是位于扩展目录（$JAVA_HOME/jre/lib/ext）中的。jar档案包。</li>
<li>用户类:开发者定义的类或者没有使用java扩展机制的第三方产品。你必须在命令行中使用-classpath 选项或者使用CLASSPATH环境变量来确定这些类的位置。我们在上面所说的用户自己的类就是特指这些类。</li>
</ol>
<p>这样，一般来说,用户只需指定用户类的位置，引导类和扩展类是 “自动 “寻找的。<br>当你的程序需要第三方的类库支持，而且比较常用，就可以采用此种方法。比如常用的数据库驱动程序，写servlet需要的servlet包等等。设置方法就是在环境变量中加入CLASSPATH.然后就可以直接编译运行了。而你的程序只用了些基础类，寻找时，就用不着必须设定它。</p>
<p>在执行Java程序的时候，会自动加载程序用中需要的在rt.jar和其他java_home\jre\lib中包含的。jar文件中包含的Java基础类库和一些扩展类库。这些都是JVM自动处理的，对用户来说是透明的。</p>
<p>如果Java程序中使用到了一些应用级别的类（如第三方的类），可以在javac和java中的-classpath选项中指定它们的搜索路径，或者是自己写ClassLoader加载，另外也可以设置ClassPath环境变量，在里面指定那些蝶阀应用级别的类的搜索路径。</p>
<p>设置ClassPath环境变量不是必须的，只是为了方便使用，设置了ClassPath，JDK就会按ClassPath制定的路径去搜索需要的应用级别的类，而不必每一次都使用-classpath选项或自己写ClassLoader。</p>
<p>还有需要注意的就是：如果相关的类就在当前工作目录下的话，上面3种方法都可以不要，因为JDK系统会首先搜索会在当前的工作目录中搜索程序相关的类。</p>
<blockquote>
<p>注意如果在class系统中两个类称是相同的（包名+类名），将会以先加载的类优先</p>
</blockquote>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
  </section>

  
  <nav class="pagination">
    <a class="extend prev" rel="prev" href="/page/8/"><i class="fa fa-angle-left"></i></a><a class="page-number" href="/">1</a><span class="space">&hellip;</span><a class="page-number" href="/page/8/">8</a><span class="page-number current">9</span><a class="page-number" href="/page/10/">10</a><span class="space">&hellip;</span><a class="page-number" href="/page/14/">14</a><a class="extend next" rel="next" href="/page/10/"><i class="fa fa-angle-right"></i></a>
  </nav>



          </div>
          


          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">kyssion</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">133</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">3</span>
                  <span class="site-state-item-name">分类</span>
                
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">2</span>
                  <span class="site-state-item-name">标签</span>
                
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">kyssion</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  

  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
